Regular expression for comma

I am trying to validate user input which is only commas. I would like to do this with RegEx, but cannot find the correct expression.

It should check the following lines (and more):

1
12
123
1,234
12,345
123,456

and invalidate the following lines (and more crazy):

1,1
1,12
12,1
12,12
123,1
123,1

Any help would be greatly appreciated.

Here is what I have tried so far (EDIT: which do not work), as well as a few options →

^(((\d{1,3},)*\d{3})|(\d{1,3}))$
^(\d{1,3}[,])*\d{3}|\d{1,3}$
+5
source share
1 answer

How about this:

^\d{1,3}([,]\d{3})*$

Basically, you can have 1-3 digits without a comma. After that you will need a comma. If you have a comma, there should be 3 more digits behind it. This comma-three-digit sequence can appear any number of times.

: , , , , , ?: :

^\d{1,3}(?:[,]\d{3})*$
+14

All Articles