Regex to allow only number between 1 to 12

tanuj shrivastava picture tanuj shrivastava · Sep 7, 2015 · Viewed 21.3k times · Source

Regex to allow only number between 1 to 12

I am trying (12)|[1-9]\d? but its not working, please help as i am new to regular expression

Answer

nu11p01n73R picture nu11p01n73R · Sep 7, 2015

Something like

^([1-9]|1[012])$
  • ^ Anchors the regex at start of the string
  • [1-9] Matches 1 to 9

  • | Alternation, matches the previous match or the following match.

  • 1[012] Matches 10, 11, or 12
  • $ Anchors the regex at the end of the string.

Regex Demo