4

I want my input tag to accept only maximum 12 comma separated values.it should not accept values like 1, mean after comma their is nothing this is the regex I have made.

My current regex is not accepting single value like 1 or a or 12ab

^[0-9a-zA-z]+(,[0-9a-zA-z]+){1,11}$

checked it on http://www.phpliveregex.com/ but it is not working. Here is my whole code

if(!preg_match("/^[0-9a-zA-z]+(,[0-9a-zA-z]+){1,11}$/", $data){
        return false
}else{
        return $data
}
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • 3
    I see that your second `z` in the RegEx is a lower-case one. You want to make it an upper-case `Z` in order to match the correct range. Which means the character groups should look like this: `[0-9a-zA-Z]` – ChristianF Jul 15 '15 at 09:32
  • Thanks ChristianF let me test it –  Jul 15 '15 at 09:39

1 Answers1

2
^[0-9a-zA-Z]+(,[0-9a-zA-Z]+){0,11}$
                             ^^ 

This should do it for you.See demo.

https://regex101.com/r/nN4oT8/4

vks
  • 67,027
  • 10
  • 91
  • 124
  • 2
    @UsamaLucky To explain why this works: You asked for a value, followed by at minimum one more value seperated by a comma. You wanted it to be followed by a minimum of 0 more values. ;) – ChristianF Jul 15 '15 at 09:29
  • Thanks vks and ChristianF let me test it –  Jul 15 '15 at 09:38