Skip to content

Instantly share code, notes, and snippets.

@perminder-klair
Created December 9, 2013 16:16
Show Gist options
  • Save perminder-klair/7874965 to your computer and use it in GitHub Desktop.
Save perminder-klair/7874965 to your computer and use it in GitHub Desktop.
Input numerical only validation
<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