Skip to content

Instantly share code, notes, and snippets.

@Reelix
Created November 23, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reelix/e93ff93bb1927a09a198a743cd89327c to your computer and use it in GitHub Desktop.
Save Reelix/e93ff93bb1927a09a198a743cd89327c to your computer and use it in GitHub Desktop.
<script>
// This function will allow 0123456789, left, right, and backspace on a textbox
// Use onkeypress="return isNumericKeyPress(event.keyCode);" to trigger
function isNumericKeyPress(keyCode)
{
return (keyCode >= 48 && keyCode <= 57 || keyCode === 8);
}
// This function will clear non-numeric values on a textbox when pasted
// Use onpaste="isNumericPaste(this);" to trigger
function isNumericPaste(tb)
{
setTimeout(function() { void(tb.value = tb.value.replace(/\D/g, '')); }, 1);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment