How can I validate the range 1-99 using a regex?
Solution 1
Off the top of my head (not validated)
^(0?[1-9]|[1-9][0-9])$
Solution 2
Here you go:
^(\d?[1-9]|[1-9]0)$
Meaning that you allow either of
- 1 to 9 or 01 to 09, 11 to 19, 21 to 29, ..., 91 to 99
- 10, 20, ..., 90
Solution 3
^(([0-9][1-9])|([1-9][0-9])|[1-9])$
should work
Solution 4
Why is regex a requirement? It is not ideal for numeric range calculations.
Apache commons has IntegerValidator with the following:
isInRange(value, 1, 99)
In addition, if you're using Spring, Struts, Wicket, Hibernate, etc., you already have access to a range validator. Don't reinvent the wheel with Regular Expressions.
Solution 5
I think it should be like...
^(0[1-9]|[1-9][0-9])$
Comments
-
Jimmy over 3 years
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
- 1
- 01
- 10
- 99
- 09
Invalid values
- 0
- 007
- 100
- 10.5
- 010
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 validAny improvements/suggestions?
-
Alin Purcaru over 13 yearsIt looks like no one else thinks backwards :)
-
Gadolin over 13 yearsI did not find it as an requirement in description, though now I see it in title. It seemed funny to go out from regex and have same thing.
-
Jander over 13 yearsI think this should be:
^(0?[1-9]|[1-9][0-9])$
. Otherwise, the ^ is bound only to the first alternative, and the $ to the second. Then again, I don't specifically know Java regexes. -
developmentalinsanity over 13 yearsHence the "not validated". I couldn't remember that sort of thing specifically.
-
user85421 over 13 yearsnote: ^ and $ are not needed if using the
Matcher.matches()
method, since it attempts to match the whole string... -
user85421 over 13 years
00
is NOT in the range 1-99 as required... and it will also match0
, which is listed as invalid. -
Slyvain over 8 yearsThis doesn't answer the question correctly. Your regex matches one of OP's invalid values:
0
-
Marcio Zabeu over 8 yearsThat's right! Thank you for seeing my answer. I've made a correction and now it's ok (edit).
-
Alan Moore almost 8 yearsNow it matches
010
. It should never match more than two digits. (The question text is ambiguous on that point, but the examples are not.) -
Alan Moore almost 8 yearsThis doesn't allow a leading zero, for numbers less than 10.
-
Alan Moore almost 8 yearsThe leading zero is supposed to be optional.
-
abhisheknirmal almost 8 years@AlanMoore Question says that Preceeding 0 is permitted, but optional
-
Alan Moore almost 8 yearsYour test is incorrect anyway; zero should not validate.
-
Alan Moore almost 8 yearsAs it is, your regex will match a number that contains a leading zero. But if you add anchors (or use the
matches()
method), it will not. And the regex must be anchored, since we're trying to validate the string. -
abhisheknirmal almost 8 years@AlanMoore Thanks for this valid explanation. I accept that regex must be anchored. Your first comment is misleading and hasty.
-
Alan Moore almost 8 yearsSorry, I meant to say the regex should also match
01
,02
,03
, etc., in addition to what it matches now. -
Ronald91 over 7 yearsI know it is supposed to be optional for this question but this solved my own issue where the leading 0 is mandatory for numbers less than 10.
-
V H about 5 yearsJust to add my two cents we originally had a pattern match for this based on 1-9 : existing pattern was
\\d?[1-9]|[1-9]0 -
this worked fine in chrome but not on IE. this current suggestion^(\\d?[1-9]|[1-9]0)\$
appears to work on both IT and chrome given thumbs up to answer