I’m unable to do or find a regex that allows me to insert in an input numbers between 0 and 100, and also allow to insert up to 2 decimals.
I can have them for separated but can’t merge them. I’m starting to think that there is an inconsistence
const VALIDATION = {
ONLY_NUMS: (value: string) => /^[0-9]*$/.test(value),
BETWEEN_0_AND_100: (value: string) => /^(d{0,2}([.,]d{1,2})?|100([.,]0{1,2})?)$/.test(value),
FLOAT_NUMS: (value: string) => /^-?[0-9]*((?<=d).(?=d))?[0-9]*$/.test(value) && value !== '-',
};
2
Answers
If your validation logic is already a function, you can parse the string using a leading
+n
or callingNumber(n)
to coerce the string into an number.Now that it’s a number, you can check if it’s
!NaN
and in the range.Compiled snippet
If you truly can’t avoid to use regexp, this one should do the trick:
It allows even decimal after 100 (like 100.34)
The below one is up to 100 with no decimal allowed after (max 99.99):