How would you go about Restricting Keyboard Input on a textbox using Jquery?
-
1I'm sorry what do you mean restrict it? Limit it to certain letters? Or stop any text from being entered?aronchick– aronchick2010-03-30 05:28:48 +00:00Commented Mar 30, 2010 at 5:28
-
Well I have a jQuery Datepicker textbox and it allows you to specify what date format you want, unfortunately it allows 33/335233/30010 when it should only allow mm/dd/yyyy so to make sure that the user doesn't put in any invalid input I am restricting input from the keyboard so the user can only select dates via the datepicker.Seth Duncan– Seth Duncan2010-03-30 05:30:40 +00:00Commented Mar 30, 2010 at 5:30
Add a comment
|
2 Answers
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.
1 Comment
eyelidlessness
+1 Given the comment (stackoverflow.com/questions/2543029/…), it seems
readonly is the way to go (it allows focus, but no input).