0

I need to validate if an input type is a number with some specifications using regex expressions. The number can have any number of commas(,) but not dots(.) but comma and dot should not be side by side such as

12,34,56,7.89 -- correct --- 1

12.34.56 -- wrong ---- 2

12345 -- correct ---- 3

123,45 -- correct ---- 4

123.45 -- correct --- 5

123,,45 -- wrong --- 6

12,345,678 -- correct --- 7

The expression I used is

((((\d+[.]?\d*)|(\d*[.]?\d+))[,]?)+\d+)

I am unable to solve test case 2 and 6. any help is acceptable thankyou

Community
  • 1
  • 1

1 Answers1

1

Try this Regex:

^\d+(?:,\d+)*(?:\.\d+)?$

Click for Demo

Explanation:

  • ^ - asserts the start of the line
  • \d+ - matches 1+ digits
  • (?:,\d+)* - matches 0+ occurrences of a , followed by 1+ digits
  • (?:\.\d+)? - matches a . followed by 1+ digits. ? in the end makes this sub-pattern optional
  • $ - asserts the end of the line
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43