开发者

Regular Expression for accepting numbers 0 to 9999

I want a regular expres开发者_JAVA技巧sion for only accepting 0-9999

No spaces, no letters. However, "blank"(empty) is allowed.


\d?\d?\d?\d?

Should do it.

Or more succinctly:

\d{0, 4}

This works because you're saying "0, 1, 2, or 3 digits", where each digit is 0-9. This allows numbers between 0 and 9999, and nothing else.

Note that it allows leading zeros, i.e. 0004 is a valid number.


This should work:

[0-9]{0,4}


well, /\d{0,4}/ is the simplest way, but generally I convert to a number and then do bounds checking


Assuming you don't want to accept numbers left-padded with zeros.

(0|([1-9]\d{0,3}))?

Read as zero or one of the following: 0 or a 1-9 followed by a 0 to 3 digit string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜