regex 1-9999 for form validation

前端 未结 4 1236

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

相关标签:
4条回答
  • 2021-01-21 16:02

    This Regex should not match numbers that start with 0 a part from 0

    Regex: /^(?!(0\d))\d{1,4}$/

    Regex (Exclude Zero): /^(?!(0))\d{1,4}$/

    Test: https://regex101.com/r/zfCKel/2

    0 讨论(0)
  • 2021-01-21 16:09

    Try the below regex,

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

    DEMO

    0 讨论(0)
  • 2021-01-21 16:09

    Try using this

    ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$
    
    0 讨论(0)
  • 2021-01-21 16:15

    This doesn't exclude zero, but /^\d{1,4}$/ should do the trick.

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