Skip to content

Instantly share code, notes, and snippets.

@iamonuwa
Created August 5, 2019 13:30
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 iamonuwa/aecb0d049ab602c496e071f6d9dbc279 to your computer and use it in GitHub Desktop.
Save iamonuwa/aecb0d049ab602c496e071f6d9dbc279 to your computer and use it in GitHub Desktop.
Allow Comma on input type text for numbers and disable text input
<Input
type="text"
placeholder="Enter the number"
onKeyDown={this.onKeyDown}
/>
onKeyDown = e => {
const keyCodeArray = [46, 8, 9, 27, 13, 110, 188, 190];
if (keyCodeArray.indexOf(e.keyCode) !== -1) {
return;
}
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment