Presto44
Presto44

Reputation: 1

Regex Numerical Range 1 - 1 million

I'm looking for a expression range for monetary purposes. It needs to be 1 - 1 million and allow commas and periods. I don't need a min/max of (, and .) for correct formatting but I would like the digits after a period to be a min/max of 2 for actual cent values. Thanks

In Range: 640 or 5,000.35 or 999,000

Not in Range: 01 or 1,000,000.01 or 333,567.678

Upvotes: 0

Views: 3065

Answers (2)

Gumbo
Gumbo

Reputation: 655559

This should do it:

^(1(\.\d{2})?|[1-9]\d{0,2}(,?\d{3})?(\.\d{2})?)|1((,000){0,2}|(000){0,2})(\.00)?$

But it would probably easier if you normalize the value first (e. g. remove any character except digits and the .) and then parse it.

Upvotes: 1

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

What I would suggest is :

  • Use something like that to verify that the input has a specific format :

    (here's a demo - http://regexr.com?30l28)

    (1[\.,])?([0-9]{1,3}[\.,])?([0-9]{1,3})([\.,][0-9]{1,2})

  • And then test the value range :

    is value<1.000.000?


My regex is by no means 100% complete, but it DOES verify your general number format though.

Upvotes: 1

Related Questions