Last active
April 27, 2016 15:18
-
-
Save swapnilshrikhande/0af37476af8c063e9c0986efd01d075e to your computer and use it in GitHub Desktop.
Validate input on key down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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