Created
August 5, 2019 13:30
-
-
Save iamonuwa/aecb0d049ab602c496e071f6d9dbc279 to your computer and use it in GitHub Desktop.
Allow Comma on input type text for numbers and disable text input
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" | |
placeholder="Enter the number" | |
onKeyDown={this.onKeyDown} | |
/> |
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
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