Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.