How would you go about Restricting Keyboard Input on a textbox using Jquery?开发者_如何学Go
If you don't want the user to be able to type anything in the text-box, then consider the readonly
and disabled
attributes. If the data in the textbox is needed at the server side, make it readonly
. If not, make it disabled
.
<input type="text" readonly="readonly" />
<input type="text" disabled="disabled" />
If you want the user to be able to type in something, but only restrict certain types of characters such as numbers only, then listen to the keyup
event on the textbox. It is fired whenever a key is released inside the text box. On this events callback, check the value of the textbox, and make changes to the value as necessary.
for <input type="text">
use attribute maxlength
精彩评论