Daarwin
Daarwin

Reputation: 3014

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:

Upvotes: 0

Views: 549

Answers (3)

Łukasz Wiatrak
Łukasz Wiatrak

Reputation: 2767

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))$

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93030

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

Upvotes: 0

stema
stema

Reputation: 92986

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

Upvotes: 0

Related Questions