I have a form text field that has a KeyUp event. On KeyUp I’m ignoring anthing but numbers, the period, backspace, delete and cursor keys. So, the only thing in the field can be a number or period.
I need to verify these things (number will become a % not over 100.00%):
-The text field string is a valid number (which should always be true unless someone puts two ‘.’s in).
-The text field string has a max of 2 decimal places
-If the text field string has a decimal point, it has something after (1 or 2 decimal places, not just a period at the end)
-The number is not larger than 100 (<= 100)
examples (what I need to verify):
95 is true (valid, decimal places not required)
95. is false (don't need a '.' with no decimal place after)
95.0 is true
95.00 is true
95.000 is false (3 decimal places not valid) 100 is true
101.01 is false (over 100)
101 is false (over 100)
I’ve seen some things that do several pieces of that list, but I’m not smart enough to modify the regex to get exactly what I need. Thanks for any help!
Some people will suggest regexes, but I think a small function is better suited for such a validation:
Live example