Skip to content

Instantly share code, notes, and snippets.

@tommylinks
Created April 13, 2021 14:35
Show Gist options
  • Save tommylinks/3f4fc9654dd84d1b0ca831059241fd59 to your computer and use it in GitHub Desktop.
Save tommylinks/3f4fc9654dd84d1b0ca831059241fd59 to your computer and use it in GitHub Desktop.
Input type text only numbers allowed (React hooks)
const inputRegex = RegExp(`^\\d*(?:\\\\[.])?\\d*$`)
const escapeRegExp = (string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
}
const handleChange = (e) => {
const string = e.target.value
if(inputRegex.test(escapeRegExp(string))) {
setInputTest(string)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment