How can I restrict users from entering special characters in the text box. I want only numbers and alphabets to be entered ( Typed / Pasted ).
Any samples?
I think checking keypress
events is not completely adequate, as I believe users can copy/paste into input boxes without triggering a keypress.
So onblur
is probably somewhat more reliable (but is less immediate).
To truly make sure characters you don't want are not entered into input boxes (or textareas, etc.), I think you will need to
keypress
(if you want to give immediate feedback) and onblur
, The code samples in the other answers will work fine for doing the client-side checks (just don't rely only on checking keypress
events), but as was pointed out in the accepted answer, a server-side check is really required.