JimO said:
I'm a javascript newbie. How can I can I check user input using the keypress
or keydown events prior to it being displayed in the textbox? What I want
to be able to do is to check the keycode to against a pre-determined string
to see if the key is valid input? If it is not it won't be displayed in the
textbox.
It's generally better to wait until they finish typing, and then
check for invalid characters. If you block characters, you open
yourself up to a couple of bad scenarios:
1) the user thinks your page is broken, and either reports it
to you (or your boss) or simply stops using it.
2) the user is typing along, accidentally hits an invalid key,
realizes it and hits the backspace. Since you already removed
the invalid key, their backspace deletes the previous character,
possibly without their noticing it.
Besides these cases, you still aren't protecting yourself from
invalid characters unless you validate the input on the server,
too.