开发者

Modyfying a regex to restrict numeric range

I am modifying a regex validator control. The regex at the moment looks like this:

(\d*\,?\d{2}?){1}$

As I can understand it allows for a number with 2 decimal places.

I need to modify it like this:

  • The number must range from 0 - 1.000.000. (Zero to one million).
  • The number may or may not have 2开发者_开发技巧 decimals.
  • The value can not be negative.
  • Comma (,) is the decimal separator.
  • Should not allow any thousand separators.


Try this regex:

^(((0|[1-9]\d{0,5})(\,\d{2})?)|(1000000(\,00)?))$

It accepts numbers like: "4", "4,23", "123456", "1000000", "1000000,00", but don't accepts: ",23", "4,7", "1000001", "4,234", "1000000,55".

If you want accept only numbers with exactly two decimals, use this regex:

^(((0|[1-9]\d{0,5})\,\d{2})|(1000000\,00))$


What about this one

^(?:\d{1,6}(?:\,\d{2})?|1000000)$

See it here on Regexr

It accepts between 1 and 6 digits and an optional fraction with 2 digits OR "1000000".

And it allows the number to start with zeros! (001 would be accepted)

^ anchors the regex to the start of the string

$ anchors the regex to the end of the string

(?:) is a non capturing group


^(([0-9]|([1-9][0-9]{1,5}))(\.[0-9]{1,2})?)|1000000$
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜