regex 1-9999 for form validation

半城伤御伤魂 提交于 2019-12-02 04:18:42

问题


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!


回答1:


Try the below regex,

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

DEMO




回答2:


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




回答3:


Try using this

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



回答4:


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



来源:https://stackoverflow.com/questions/24914114/regex-1-9999-for-form-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!