Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active April 27, 2016 15:18
Show Gist options
  • Save swapnilshrikhande/0af37476af8c063e9c0986efd01d075e to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/0af37476af8c063e9c0986efd01d075e to your computer and use it in GitHub Desktop.
Validate input on key down
<input class="input-field" />
$(".input-field").on("keyup blur",function(event){
var valStr = $(this).val();
if($.trim(valStr) == '.' || $.trim(valStr) == '-'){
return true;
}
if(isNumeric(valStr)){
return true;
} else {
var value = parseFloat(valStr,10);
if(value){
$(this).val(value);
} else {
$(this).val("");
}
}
});
function isNumeric(num){
return !isNaN(num)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment