Search code examples
phpregexpreg-match

textarea to accept 12 comma separated values for input type text


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
}

Solution

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

    This should do it for you.See demo.

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