• Status: Solved
• Priority: Medium
• Security: Public
• Views: 342

# 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?
0
mmedi005
• 4
• 4
• 3
• +1
3 Solutions

Commented:
This should work.
``````(^\d*\,?\d*[0-9]+\d*\$)|(^[0-9]+\d*\,\d*\$)
``````
0

Commented:
Try:
``````^\d{1,3}|\d{1,3}(?:,\d{3})*\$
``````
0

Commented:
Actually, let's flip that around:
``````^\d{1,3}(?:,\d{3})*|\d{1,3}\$
``````
0

Commented:
@professionalcomputersolutions

That didn't work for the provided example (1,000,000).
0

Author Commented:
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?
0

Author Commented:
e.g.  they should be allowed to enter:

1,000,000

or

1000000
0

IT GuruCommented:
The expression:
^\d{1,3}(?:,\d{3})*|\d{1,3}\$
will match the 100 out of the value:
100.00
because the ^ and \$ need to exclude from the alternatives specified with the | like this:
^(?:\d{1,3}(?:,\d{3})*|\d{1,3})\$

However, the |\d{1,3} part of the expression is redundant anyway. I think what you want is:
``````^(?:\d{1,3}(?:,\d{3})*|\d+)\$
``````
0

IT GuruCommented:
"because the ^ and \$ need to exclude from the alternatives" should read
"because the ^ and \$ need to be excluded from the alternatives"
0

Author Commented:
Perfect,

Thank you all
0

IT GuruCommented:
Note that if you want to be more relaxed about where the commas go, you could just use something like this:
^\d[,\d]*\$
0

Author Commented:
I think making them use the commas correctly is better, but thank you again...helped a lot
0

Commented:
@TerryAtOpus

will match the 100 out of the value:

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

## Featured Post

• 4
• 4
• 3
• +1
Tackle projects and never again get stuck behind a technical roadblock.