sharmacal
sharmacal

Reputation: 457

number with decimal places to allow commas in regular expression validation

I am using a regular expression in javascript as

/^\s*?(\d+(\.\d{1,2})?|\.\d{1,2})\s*$/

This is used to check for numbers upto 2 decimal places, like 200.45.

I also need to allow more than one commas like 1,000.45 or 2,00,455.76

Please note this expression also allows numbers like .1 , 1.2 .

How can this be achieved the above regular expression. Thanks

Upvotes: 1

Views: 3135

Answers (3)

sharmacal
sharmacal

Reputation: 457

Thanks I got the above expression working as.

var regexp =/^\s*?([\d\,]+(\.\d{1,2})?|\.\d{1,2})\s*$/;

Upvotes: 2

Madhusudan Joshi
Madhusudan Joshi

Reputation: 4476

Here's what i can think of the regular expression : ^(\d?\d?\d(,\d\d\d)*|\d+)(\.\d\d)?$

I have not run it, but you can try it out.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

Try this pattern:

/\b\d{1,3}(,\d{3})*(\.\d{1,2})?\b/

Upvotes: 0

Related Questions