0

I want to validate the passcode input field exclude special characters. Basically i just want to allow letters or numbers for some reason is not detecting characters like `, ´. ç, etc.

$('#passcode').on('keypress', function(e) {
    var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (!/^[A-Z0-9]+$/i.test(key)) {
        e.preventDefault();
    }
})

Here is a fiddle: https://jsfiddle.net/gb5kc12z/

Try with % or ´, ç, `

handsome
  • 2,335
  • 7
  • 45
  • 73
  • 1
    I can't reproduce the problem. Of course, I had to make some assumptions about the HTML you are using. You didn't provide a [mcve] in your question. – Quentin Nov 26 '16 at 23:33
  • 1
    I'm curious: are you storing that passcode in plaintext? Because there is literally no other reason to limit a password-like input, and this reason isn't a good one either because you should be hashing your passwords with bcrypt, argon or PBKDF2, in which case it makes no difference what values are in your password. – Nzall Nov 27 '16 at 00:15
  • can't reproduce your bug, it work fine for me; +1 @Nzall – Blag Nov 27 '16 at 00:17

1 Answers1

0

I'm guessing you're testing by pasting in those special characters instead of typing them? Your code only tests when a user releases a key to type in a character. See Best way to track onchange as-you-type in input type="text"? for more detail on how to track all value change events on an input field in modern browsers.

Community
  • 1
  • 1
Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23