https://www.phpjabbers.com/php-validation-and-verification-php27.html
Now we have to set this class to apply for every character that we enter. So we add a (+) plus sign after our class definition. We are still missing something. We have not defined the range of our validation test. We have to set which part of the text we are validating. If we don’t do this our regular expression will be satisfied if it finds even one match in the characters that we enter, which is of no use for us. How do we do this? We put our string between /^$/ start and end characters. “^” means the start of the line and “$” means the end of it. We are ready to build our regexp.
/^[a-zA-Z -]+$/ The forward slash is used by preg_match to define the start and the end of our regexp.
Now we are finished, are we? There is just one more thing to do. The way that we defined our class allows the user to enter dash at the beginning of the name. This is something we want to prevent. So we have to add something to our regexp, so it will disallow this.
[A-Z] We define a new class for the first letter of the user name. It can contain only upper case letters.
Now we combine what we have done so far, to get the final result. The return of preg_match() is 0 if there isn’t a match. In that case we have to set our error variable, so we can show some meaningful message to the user.
/^[A-Z][a-zA-Z -]+$/