Regex accept numeric only. First character can't be 0

前端 未结 4 1071
长情又很酷
长情又很酷 2020-12-13 20:17

I have a textbox that I created using .NET..

By using that textbox, the user only can key in numeric. But not start with 0. start with 1-9. after the user key in th

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 21:09

    To match a number starting with any digit but zero:

    ^[1-9][0-9]*$
    

    And if you want to match 0 as well:

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

    remove the last plus if you want a single zero only

    To allow any alpha-numeric after first non-zero:

    ^[1-9a-zA-Z][0-9a-zA-Z]*$
    

提交回复
热议问题