I’m trying to validate and get an “interest rate” from user input.
I’m wanting to accept 1% -> 99%, with the ability for 0, 1 or 2 decimals. I also dont mind if they include a “%”.
i.e.
- 3.2
- 3.45%
- 44%
- 4.00%
The best I could find was
/([0-9]{1,2})%/
But thats as close as I could get… any help would be greatly appreciated.
Explanation on the regex: it must start (^) with one or two digits (\d{1,2}). Then, you can have one group that starts with a . (although, in the Netherlands, a comma would be used). Then, it must be followed by one or two digits. (.(\d{1,2})). That group is optional (?). Then, there’s an optional percent mark (%?) and the string should end there ($).
EDIT: You also might want to use a (faster?) non-regex alternative: