How can I validate the range 1-99 using a regex?

58,819

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. 1 to 9 or 01 to 09, 11 to 19, 21 to 29, ..., 91 to 99
  2. 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])$
Share:
58,819
Jimmy
Author by

Jimmy

#SOreadytohelp

Updated on October 18, 2020

Comments

  • Jimmy
    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. 1
    2. 01
    3. 10
    4. 99
    5. 09

    Invalid values

    1. 0
    2. 007
    3. 100
    4. 10.5
    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 valid

    Any improvements/suggestions?

  • Alin Purcaru
    Alin Purcaru over 13 years
    It looks like no one else thinks backwards :)
  • Gadolin
    Gadolin over 13 years
    I 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
    Jander over 13 years
    I 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
    developmentalinsanity over 13 years
    Hence the "not validated". I couldn't remember that sort of thing specifically.
  • user85421
    user85421 over 13 years
    note: ^ and $ are not needed if using the Matcher.matches() method, since it attempts to match the whole string...
  • user85421
    user85421 over 13 years
    00 is NOT in the range 1-99 as required... and it will also match 0, which is listed as invalid.
  • Slyvain
    Slyvain over 8 years
    This doesn't answer the question correctly. Your regex matches one of OP's invalid values: 0
  • Marcio Zabeu
    Marcio Zabeu over 8 years
    That's right! Thank you for seeing my answer. I've made a correction and now it's ok (edit).
  • Alan Moore
    Alan Moore almost 8 years
    Now 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
    Alan Moore almost 8 years
    This doesn't allow a leading zero, for numbers less than 10.
  • Alan Moore
    Alan Moore almost 8 years
    The leading zero is supposed to be optional.
  • abhisheknirmal
    abhisheknirmal almost 8 years
    @AlanMoore Question says that Preceeding 0 is permitted, but optional
  • Alan Moore
    Alan Moore almost 8 years
    Your test is incorrect anyway; zero should not validate.
  • Alan Moore
    Alan Moore almost 8 years
    As 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
    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
    Alan Moore almost 8 years
    Sorry, I meant to say the regex should also match 01, 02, 03, etc., in addition to what it matches now.
  • Ronald91
    Ronald91 over 7 years
    I 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
    V H about 5 years
    Just 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