user2712882
user2712882

Reputation: 81

regex to include everything except numbers; to include everything except letters

I'm using /^\d+$/ for numbers and /^[a-zA-Z]*$/ for letters.

These are used to validate the characters entered into a text box, using onkeydown="somefunction(String.fromCharCode(event.keyCode));" and my argument is passed on to see if it matches the regex. I want to, however, include backspace for both of those. Maybe even the keyboard arrows, but arrows are not the most important. Please help!

Upvotes: 0

Views: 112

Answers (1)

Wes Hardaker
Wes Hardaker

Reputation: 22262

Your problem is that you're trying to validate each key press rather than the whole field. Keypresses can be all sorts of things, including backspaces, deletes, and arrows as you've mentioned but there are a slew of others as well.

Instead, build your function to validate the completed form field instead. Use the onchange="somefunction()" instead to test the contents of the form field in question.

Upvotes: 1

Related Questions