phpwev
phpwev

Reputation: 29

Enable SHIFT key

I have an input where user's firstname will be put on. I disabled all keys EXCEPT a-z and 0-9 and it works fine. I want to allow SHIFT key too so when the user wants to capitalize a letter he can do it. I already tried some solutions but doesnt work.

$('#firstname').on('keypress', function(e){
  return e.metaKey ||
    e.which <= 0 ||
    e.which == 8 ||
    /[a-z 0-9]/.test(String.fromCharCode(e.which));
})

Upvotes: 1

Views: 326

Answers (1)

Kevin B.
Kevin B.

Reputation: 1363

You can just add the character range to you regular expression.

$('#firstname').on('keypress', function(e){
  return e.metaKey ||
    e.which <= 0 ||
    e.which == 8 ||
    /[a-zA-Z0-9 ]/.test(String.fromCharCode(e.which));
})

Upvotes: 1

Related Questions