Skip to content

Instantly share code, notes, and snippets.

@Jenya476
Created March 25, 2019 09: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 Jenya476/c5c6f899e931b72a927f6529916d893b to your computer and use it in GitHub Desktop.
Save Jenya476/c5c6f899e931b72a927f6529916d893b to your computer and use it in GitHub Desktop.
floatValidator(event) {
const value = event.target.value;
const key = event.key;
const newValue = value + key;
const patternZeroInBeginning = /^0+(\d)/;
const patternNumber = /\d/;
if ((key !== '.' && !patternNumber.test(key)) || value && key === '.' && value.indexOf('.') > 0) {
event.preventDefault();
return;
}
if (!value && key === '.') {
event.preventDefault();
event.srcElement.value = '0.';
} else if (patternZeroInBeginning.test(newValue)) {
event.preventDefault();
event.srcElement.value = key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment