Created
December 9, 2013 16:16
-
-
Save perminder-klair/7874965 to your computer and use it in GitHub Desktop.
Input numerical only validation
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 type='text' onkeypress='validate(event)' /> | |
<script> | |
function validate(evt) { | |
var theEvent = evt || window.event; | |
var key = theEvent.keyCode || theEvent.which; | |
key = String.fromCharCode( key ); | |
var regex = /[0-9]|\./; | |
if( !regex.test(key) ) { | |
theEvent.returnValue = false; | |
if(theEvent.preventDefault) theEvent.preventDefault(); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment