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

后端 未结 11 1298
忘掉有多难
忘掉有多难 2021-02-07 03:00

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 opt

相关标签:
11条回答
  • 2021-02-07 03:41

    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
    0 讨论(0)
  • 2021-02-07 03:46

    Just do:

    ^([0]?[1-9]{1,2})$
    

    The range will be set from 0 to 9 and the {1,2} means min digits = 1 and max digits = 2.

    It will accept, for example: 0, 00, 01, 11, 45, 99, etc... It will not accept, for example: 000, 1.2, 5,4, 3490, etc...

    0 讨论(0)
  • 2021-02-07 03:48
    ^(([0-9][1-9])|([1-9][0-9])|[1-9])$
    

    should work

    0 讨论(0)
  • 2021-02-07 03:50

    Off the top of my head (not validated)

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

    0 讨论(0)
  • 2021-02-07 03:52

    This one worked for myself:

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

    It checks for 10-99 or 1-9 => 1-99 with one leading zero allowed

    0 讨论(0)
  • ^[0-9]{1,2}$ 
    

    should work too (it'll will match 00 too, hope it's a valid match).

    0 讨论(0)
提交回复
热议问题