I need to validate some user input, to ensure a number entered is in the range of 1-99 inclusive. These must be whole (Integer) values
Preceeding 0 is permitted, but optional
Valid values
Invalid values
So far I have the following regex that I've worked out : ^0?([1-9][0-9])$
This allows an optional 0 at the beginning, but isn't 100% correct as 1
is not deemed as valid
Any improvements/suggestions?
\d for single or multiple digit numbers It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range. [1-9][0-9] will match double digit number from 10 to 99.
You can use regular expressions to match and validate the text that users enter in cfinput and cftextinput tags. Ordinary characters are combined with special characters to define the match pattern. The validation succeeds only if the user input matches the pattern.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
Off the top of my head (not validated)
^(0?[1-9]|[1-9][0-9])$
Here you go:
^(\d?[1-9]|[1-9]0)$
Meaning that you allow either of
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With