Home > Mobile >  Regex that accept 2 digit integer seperated by comma
Regex that accept 2 digit integer seperated by comma

Time:05-14

I want to validate a html form, that accept only maximum 2 digit number, seperated by comma. Example:-

34,73,15,3,88,4,97,6,9,76,20

I had tried /^([0-9]{2})$/ but don't know how to seperate with comma.

CodePudding user response:

You can do it using the following regex:

^(\d\d?,)*\d\d?$

Explanation:

  • ^: start of string
  • (\d\d?,)*: a digit (optionally followed by another digit), followed by comma
  • \d\d?: a digit (optionally followed by another digit)
  • $: end of string

Try it here.

CodePudding user response:

Try this. Two digits, then 0 or more times "comma and two digits".

^\d\d(,\d\d)*$

Instead of \d\d you can use \d{2}

  • Related