Jimmy Jimmy - 5 months ago 35
Java Question

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

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?

Answer

Off the top of my head (not validated)

^(0?[1-9]|[1-9][0-9])$