Search code examples
regexnumbers

regex 1-9999 for form validation


I am trying to write some form validation, I need one of the inputs to be 1-9999. I know nothing about regular expressions ( never used them before) and here is my first attempt

/^([1-9][1-9]|[1-9]|[1-9]\d|9999)$/

Does not seem to want to work, can anyone help me? Thanks!


Solution

  • Try the below regex,

    ^(?:[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[1-9])$
    

    DEMO