Regex: how to only allow integers greater than zero

后端 未结 6 1000
失恋的感觉
失恋的感觉 2021-01-20 00:42

I have tried the following to only allow integers in my text box, this works great but it allows a zero in there. Is there anything else I can add to prevent a zero being ad

6条回答
  •  悲&欢浪女
    2021-01-20 00:42

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

    This will allow "silly" numbers like 007 as well, but not 0 or 000 or an empty string.

    Note that \d matches also digits from other character sets like ٠١٢٣٤٥٦٧٨٩. See: \d is less efficient than [0-9].

    ^ denotes the start, $ the end of the string. Together they ensure that the whole string is matched.

提交回复
热议问题