I have the following script:
$('#txtUsername').keydown( function(evt) {
if (/^[a-z0-9]+$/i.test(String.fromCharCode(evt.keyCode)) == false) {
evt.returnValue = false;
return false;
}
});
The script above is supposed to prevent the user from entering in special character inside the user name field. The problem is that this script allows typing of alphanumeric characters and it blocks `~_+-=[]{}\|;:'",<.>/?
I need this to also block and !@#$%^&*() characters.
What am I doing wrong? I am testing this with latest Chrome browser.
EDIT I know this is not the best way to validate a username, but I am questioning why regex is not showing the correct behavior.
jsFiddle click here