-4

I need to validate an input filter on a form because I cannot format the output due to the template I am using. This is a price field and I need the output to be uniform. I am able to use a regular expression to validate the input.

I would like regex to be a whole number with thousands separated with a comma. No decimal. No $.

Valid:

* 0 (just zero) 
* 100
* 1,000 
* 10,000

Not Valid:

  • 01 (leading zero)
  • 100.50
  • 1000
  • $10,000
  • -10,000 (no negative number)

Does anyone know how to do this? I cannot find this and thought it should be a common regex.

Jean-François Corbett
  • 34,562
  • 26
  • 126
  • 176
Donald Ax
  • 3
  • 1

2 Answers2

0

You could use this regex: ^(0|[1-9][0-9]{0,2})(,[0-9]{3})*$

Below is a python implementation:

import re
regex = r"^(0|[1-9][0-9]{0,2})(,[0-9]{3})*$"
num = "10,000"
if re.search(regex, num):
  print(True)
else:
  print(False)
shadan
  • 28
  • 3
0

I believe you need something similar to,

Using javascript

^(0|[1-9][0-9]{0,2})(,[0-9]{3})*$