We have defined this regular expresion [a-zA-Z0-9-] and implemented an onkeypress event to restrict the input, aparently works well, but when using a spanish keyboard it allows to insert the á é í ó ú character, which are generated pressing 2 keys and the implemented solution does not block this input. Obiously the keypress event should not be used in this cases. Does any one have an idea of a better approach to implement this restriction?. The requirement is to avoid the input of characters other than [a-zA-Z0-9-] but character by character
Asked
Active
Viewed 222 times
1 Answers
0
Key event handler regex have to be anchored. Try ^[a-zA-Z0-9-]+$
-
The problem i am facing is: keypress event is not being triggered when deadacute key is pressed, you know á é í ó ú are generated pressing deadacute key and then a e i o u. keypress event does not work in those cases – jvacaq Oct 06 '14 at 20:38
-
I do mostly MFC, but after some reading, I'll take a guess. Every key should have a keydown/keyup event, even dead keys. Try handling those events, look for `"U+0302" (Combining Circumflex Accent key)` the dead key you speak of. If found do a call to preventDefault() on that element for that key only. – Oct 07 '14 at 15:16
-
Also there might be a workaround here - http://stackoverflow.com/questions/10721499/differency-normal-characters-and-half-characters-on-keydown – Oct 07 '14 at 15:27