How would you go about Restricting Keyboard Input on a textbox using Jquery?
+3
A:
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.
Anurag
2010-03-30 05:32:29
+1 Given the comment (http://stackoverflow.com/questions/2543029/restrict-keyboard-input-jquery#comment-2543967), it seems `readonly` is the way to go (it allows focus, but no input).
eyelidlessness
2010-03-30 05:38:45