Skip to content

Instantly share code, notes, and snippets.

@edtoken
Created May 29, 2014 22:38
Show Gist options
  • Save edtoken/b09ead4a5a6e5b7bc8b9 to your computer and use it in GitHub Desktop.
Save edtoken/b09ead4a5a6e5b7bc8b9 to your computer and use it in GitHub Desktop.
$(document).on('keyup', '.numeric', function(e){
var val = false;
var sKeys = [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 144];
var re = /^[0-9]*$/;
var text = String.fromCharCode(e.keyCode);
if(this.value){
val = this.value;
}else if(this.innerHTML){
val = this.innerHTML;
}
if(!re.test(text) && val !== '' && sKeys.indexOf(e.keyCode) < 0 || (val !== false && !re.test(val))){
$(this).addClass('input-error');
return false;
}
$(this).removeClass('input-error');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment