Skip to content

Instantly share code, notes, and snippets.

@karmiphuc
Created January 16, 2014 02:17
Show Gist options
  • Save karmiphuc/8448724 to your computer and use it in GitHub Desktop.
Save karmiphuc/8448724 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;
}
});
@AlexPashley
Copy link

Nice cheers :) I removed 32 from allowed array for spaces 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment