1

Possible Duplicate:
PHP regex to check a English name

I'm trying to write a regular expression that will check a name field that i have for a user.

I want it to check for

Firstname SPACE Middle initial SPACE Last name.

I know for singular letters for the middle intitial I'll need [a-z] but I'm not sure how to do the full words for first and last name and the spaces in between.

Community
  • 1
  • 1
harry
  • 23
  • 1
  • 3
  • Are all three parts required every time? – Ian McLaird Jul 17 '11 at 17:43
  • `[a-z]` won't match middle initials (since they are usually upper case!) not will it match any that have an accent. – Quentin Jul 17 '11 at 17:49
  • 3
    Or what about [Regex get first name out of “Aardal, Prof.dr.ir. K.I. (Karen)”](http://stackoverflow.com/questions/5351234/regex-get-first-name-out-of-aardal-prof-dr-ir-k-i-karen) -- Or what about [regex to parse first and last name](http://stackoverflow.com/questions/5994870/regex-to-parse-first-and-last-name) -- And this seems similar as well: [How can I validate a name field to allow only one space, and begin and end with a letter in PHP?](http://stackoverflow.com/questions/6118670/how-can-i-validate-a-name-field-to-allow-only-one-space-and-begin-and-end-with-a) – hakre Jul 17 '11 at 17:49
  • 2
    For the listing: [Regex for names with special characters (Unicode)](http://stackoverflow.com/questions/5963228/regex-for-names-with-special-characters-unicode/5963425#5963425) (hat tip Kristoffer la Cour +1) – hakre Jul 17 '11 at 17:58

3 Answers3

2

You might want to use str_word_count() to make it a bit easier for your self.

Also, to check for names you might want to add more letters than just a-z, you should take a look at the answer in this question.

Community
  • 1
  • 1
Kristoffer la Cour
  • 2,591
  • 3
  • 25
  • 36
1

Name may contain - symbol. /^[\-a-z]+\s[\-a-z]\s[\-a-z]+$/i

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Londeren
  • 3,202
  • 25
  • 26
0
^[A-Za-z]+\s[A-Za-z]\s[A-Za-z]+$
VMAtm
  • 27,943
  • 17
  • 79
  • 125