Search code examples
phpregexvalidationletter

Regex to require at least 2 letters in string


$pattern = "/[a-z]*[a-z]*/i";
if (!preg_match($pattern, $value)){
    $this->error_name = "The name should contain at least two letters."; 
}

I am trying to check if the user types his name with at least two letters. So basically, he can't enter his name as such 111111111111. it must have two letters.

The regular expression that I wrote doesnt work..why?


Solution

  • $pattern="/[a-z].*[a-z]/i";
    if(!preg_match($pattern, $value)){
       $this->error_name="The name should contain at least two letters."; 
    }
    

    Your code didn't work because * means zero or more times, so it would also match none or one character.