I've seen plenty of examples here on how to restrict to only Numbers, and I'm using the correct key codes for a-z and A-Z. Do you see what I'm doing wrong?
回答1:
The property event.key gave me an undefined value. Instead, I used event.keyCode:
function alphaOnly(event){var key =event.keyCode;return((key >=65&& key
Note that the value of 8 is for the backspace key.
回答2:
Rather than relying on key codes, which can be quite cumbersome, you can instead use regular expressions. By changing the pattern we can easily restrict the input to fit our needs. Note that this works with the keypress event and will allow the use of backspace (as in the accepted answer). It will not prevent users from pasting 'illegal' chars.
回答3:
On newer browsers, you can use:
You can use regular expressions to restrict the input fields.