Search code examples
htmlregexvalidationinput

How to match numbers ranging from 1 to n with regex?


I want to validate, using HTML pattern attribute (so i can get the notification), a numeric string that must only contain numbers ranging from 1 to 400 (I can allow 001).

I tried the following pattern but i'm missing a few many

^([1-3][0-9]{0,2}|400)$

Solution

  • Try this one ([1-9][0-9]{0,2}|400)$

    [1-9][0-9]{0,2} matches any number between 1–999
    |400 limit upto 400