Dman100
Dman100

Reputation: 747

RegEx match for thousands currency value

I'm using the jquery validation plug-in and have added a method to check for currency. Here is the method:

j$.validator.addMethod("money", function(value, element) {
            return this.optional(element) || /^(\d{1,3})(\.\d{2})$/.test(value);
            //return this.optional(element) || /^((?:\d{1,3},)?\d{1,3})(\.\d{2})$/.test(value);
        }, "Must be in US currency format 0.99");

This correctly validates values from 0.00 to 999.99. But, as soon as I hit 1,000.00 the method will return false. I'm not very good with regular expressions, but from what I can tell, the regex expression does not match a value that is 1,000.00 or higher?

Can anyone help with how to modify the regex expression to match a currency value for 0.00 up to 999,999.99? So, it can correctly validate a currency value in the thousands?

Thanks for any help.

Upvotes: 0

Views: 2683

Answers (2)

anubhava
anubhava

Reputation: 785058

Try this regex:

^\d{1,3}(?:,\d{3})?(?:\.\d{2})?$

Live Demo

Upvotes: 2

user219882
user219882

Reputation: 15844

This will validate every range starting from 0.00

^\d{1,3}([,]\d{3})*(\.\d{2})$

and if you want to have it only to 999,999.99 then use this

^\d{1,3}([,]\d{3})?(\.\d{2})$

Upvotes: 0

Related Questions