Kardinal
Kardinal

Reputation: 493

regex for comma delimited 6-digit values

I'm trying to write a validation javascript regex to check a string is a comma delimited 6-digit string. For example:

123456

OR

123456,123456,123456

but NOT

123456,123

The regex I'm using is ^([0-9]{6})(\,[0-9]{6})*. The problem I'm having is how to get the regex to fail on the last part, where it should fail since 123 is not a 6-digit number. I tired adding a $ to the end of the regex, but then it breaks the whole thing. Can someone help me so that the regex will FAIL unless the string is A) a 6-digit string or B) comma delimited 6-digit strings?

Upvotes: 3

Views: 515

Answers (2)

Danilo Toro
Danilo Toro

Reputation: 597

try with this:

\d{6}(,\d{6}){0,}

In this page, it's work

https://regex101.com/

Upvotes: 1

pguardiario
pguardiario

Reputation: 55012

You were close but you needed a $:

^\d{6}(,\d{6})*$

Upvotes: 3

Related Questions