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

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
Asked:
mmedi005
  • 4
  • 4
  • 3
  • +1
3 Solutions
 
Tyler LaczkoCommented:
This should work.
(^\d*\,?\d*[0-9]+\d*$)|(^[0-9]+\d*\,\d*$)

Open in new window

0
 
käµfm³d 👽Commented:
Try:
^\d{1,3}|\d{1,3}(?:,\d{3})*$

Open in new window

0
 
käµfm³d 👽Commented:
Actually, let's flip that around:
^\d{1,3}(?:,\d{3})*|\d{1,3}$

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
käµfm³d 👽Commented:
@professionalcomputersolutions

That didn't work for the provided example (1,000,000).
0
 
mmedi005Author 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
 
mmedi005Author Commented:
e.g.  they should be allowed to enter:

1,000,000

or

1000000
0
 
Terry WoodsIT 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+)$

Open in new window

0
 
Terry WoodsIT GuruCommented:
"because the ^ and $ need to exclude from the alternatives" should read
"because the ^ and $ need to be excluded from the alternatives"
0
 
mmedi005Author Commented:
Perfect,

Thank you all
0
 
Terry WoodsIT 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
 
mmedi005Author Commented:
I think making them use the commas correctly is better, but thank you again...helped a lot
0
 
käµfm³d 👽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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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