Created
May 21, 2013 10:38
-
-
Save swape/5618896 to your computer and use it in GitHub Desktop.
Limiting an input field to only accept numbers
This file contains 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
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(); | |
} | |
} | |
//<input type="number" name="mynumber" value="" placeholder="number" onkeypress="validate(event)" id="number-0" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment