Mike Perrenoud
Mike Perrenoud

Reputation: 67898

RegEx for currency missing single digit numbers?

I have the following RegEx ...

(\d*,?){1,5}\.?\d{2}

... and it will successfully match everything execpt the single digit 9's in this listing ...

9
99
999
9,999
99,999
999,999
9,999,999
99,999,999
999,999,999

9.99
99.99
999.99
9,999.99
99,999.99
999,999.99
9,999,999.99
99,999,999.99
999,999,999.99

9
99
999
9999
99999
999999
9999999
99999999
999999999

9.99
99.99
999.99
9999.99
99999.99
999999.99
9999999.99
99999999.99
999999999.99

... can you help me match those as well? Maybe the entire RegEx is wrong and I'm just getting lucky?

Usage

I'm using this in a client-side RegularExpressionValidator.

Technology

ASP.NET Web Form 4.0

Upvotes: 2

Views: 350

Answers (1)

Rawling
Rawling

Reputation: 50114

At the moment you're seeking some digits and/or commas, followed by an optional point, followed by exactly two (non-optional) digits. You're also allowing e.g. 99,.99.

Try

\d+(,\d{3})*(\.\d{2})?

which is at least one digit, followed by zero-or-more groups of (comma digit digit digit), followed by an optional group of (point digit digit).

Upvotes: 4

Related Questions