Link to home
Start Free TrialLog in
Avatar of mmedi005
mmedi005

asked on

Looking for a regular expression for a validator that accepts commas in a number but i don't want the decimal places....

Looking for a regular expression for a validator that accepts commas in a number but i don't want the decimal places....

i want them to be able to put 1,000,000

but not 1,000,000.00

I want whole numbers only for a validation control

How can I achieve this?
SOLUTION
Avatar of Tyler Laczko
Tyler Laczko
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
Try:
^\d{1,3}|\d{1,3}(?:,\d{3})*$

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@professionalcomputersolutions

That didn't work for the provided example (1,000,000).
Avatar of mmedi005
mmedi005

ASKER

Actually, let's flip that around:

1:^\d{1,3}(?:,\d{3})*|\d{1,3}$

That worked but I need it to work either with or without commas....can this be done?
e.g.  they should be allowed to enter:

1,000,000

or

1000000
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"because the ^ and $ need to exclude from the alternatives" should read
"because the ^ and $ need to be excluded from the alternatives"
Perfect,

Thank you all
Note that if you want to be more relaxed about where the commas go, you could just use something like this:
^\d[,\d]*$
I think making them use the commas correctly is better, but thank you again...helped a lot
@TerryAtOpus

will match the 100 out of the value:

Good catch. I indeed forgot to group the expression. I must be getting older  = )