If you are trying to create a rule on mentioned below point:

  • Only letters a through z and spaces
  • Minimum 2 letters
  • Maximum 30 letters
  • Each word must be at least 2 letters
  • First letter of each word capital

Use the following expression:

^(?=[a-zA-Z ]{2,30}$)[A-Z][a-z]+(?:[ ]+[a-zA-Z][a-z]+)*$

Example:

$len = strlen($input)
if ($len < 2 || $len > 40){
    // report error length
}else if (!preg_match('/ ^(?=[a-zA-Z ]{2,40}$)[A-Z][a-z]+(?:[ ]+[a-zA-Z][a-z]+)*$/', $input)){
    // report error pattern
}else{
    // process input
}
Your Answer
Sign In
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.