Skip to content

Instantly share code, notes, and snippets.

@ekahialoha
Forked from karmiphuc/CheckKeypress.js
Created April 30, 2020 15:25
Show Gist options
  • Save ekahialoha/987ff167762c3e0169bc4fae160c7f4a to your computer and use it in GitHub Desktop.
Save ekahialoha/987ff167762c3e0169bc4fae160c7f4a to your computer and use it in GitHub Desktop.
Keypress Validation ALLOW CHARACTERS: a..z A..Z 0..9 - _ . , @ Reference keycode table: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
$('input').bind('keypress', function(e) {// Kami.2013.12.24.10:57 Allow only letters, numbers, and @ , . - _
var allowedCode = [8, 13, 32, 44, 45, 46, 95];
var charCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode :
((e.which) ? e.which : 0));
if (charCode > 31 && (charCode < 64 || charCode > 90) &&
(charCode < 97 || charCode > 122) &&
(charCode < 48 || charCode > 57) &&
(allowedCode.indexOf(charCode) == -1)) {
e.preventDefault();
$('.alert-box.onerror').html('<h3>ALLOW CHARACTERS: a..z A..Z 0..9 - _ . , @</h3>').fadeIn();
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment