0

How would you go about Restricting Keyboard Input on a textbox using Jquery?

2
  • 1
    I'm sorry what do you mean restrict it? Limit it to certain letters? Or stop any text from being entered? Commented 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. Commented Mar 30, 2010 at 5:30

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Given the comment (stackoverflow.com/questions/2543029/…), it seems readonly is the way to go (it allows focus, but no input).
0

for <input type="text"> use attribute maxlength

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.