I want to use an if-statement to run code only if the user types in a letter or a number.
I could use
if (event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || ...) {
// run code
}
Is there an easier way to do this? Maybe some keycodes don’t work in all web browsers?
If you want to check a range of letters you can use greater than and less than:
For a more dynamic check, use a regular expression:
See the MDC documentation for the
keyCodeproperty, which explains the difference between that and thewhichproperty and which events they apply to.