tags:

views:

145

answers:

4

Looking for a regex to check a valid English name, i.e.:

  • A-Z, a-z, space only
  • First name, (optional) Middle name, Last name

An acceptable example: John von Neumann

Thanks!

EDIT (added checking code):

#!/usr/bin/php

<?php

    function check_name($regex, $name)
    {
        $rc = 'Bad';
        if (preg_match($regex, $name)) {    
            $rc = 'Good';
        }
        return '|' . $name . '| ' . $rc . "\n";
    }

    $regex = '/(?:[A-Za-z]+(?:\s+|$)){2,3}/';
    echo check_name($regex, 'First');
    echo check_name($regex, 'First Last');
    echo check_name($regex, 'Two  Spaces');
    echo check_name($regex, 'First Middle Last');
    echo check_name($regex, 'First Middle Last Fourth');
    echo check_name($regex, 'First 123');

?>
+2  A: 

Your name is going to fail for the O'Neil's out there. Also the Webb-Smiths and the like.

Use @Amber's modified:

/(?:[A-Za-z.'-]+\s*){2,3}/

hopeseekr
What about all the O’Neils of the world?
hb2pencil
Man! that's an XSS just waiting to happen.
hopeseekr
A: 

You can try:

if(preg_match('/^([a-z]+ ){1,2}[a-z]+$/i',trim($input))) {
  // valid full name.
}

As noted by others, it will allow NO punctuations in the name!!

codaddict
This will fail on any names longer than single characters, because it has no variable-length specifiers (`*` or `+`).
Amber
@Amber: Thanks for pointing.
codaddict
actually, it's a horrible solution as it stands, because it wont even handle camelcased names like Sam.
hopeseekr
@hopeseekr: What makes you think it won't ?
codaddict
@codaddict bah, i saw the /i. Man, why do it that way? I'm probably not the only one to make that incorrect assumption.
hopeseekr
+5  A: 

If the rules you've stated are actually what you want, the following would work:

/^(?:[A-Za-z]+(?:\s+|$)){2,3}$/

However, there are quite a few real-life names that don't fit in these rules, like "O'Malley", "Jo-Jo", etc.

You can of course extend this regex fairly easily to allow other characters - just add them into the brackets. For instance, to allow apostrophes and dashes, you could use [A-Za-z'-] (the - has to be at the end, or it will be interpreted as a range).

Amber
+1: For providing working regex for the OP requirement rules.
shamittomar
If you add the rules to allow for names like Teal'c Malquet, Jack O'Neill and Jennifer McKay-Keller ;-)
hopeseekr
@Amber thanks! It seems that the regex allows a single string (e.g. `John` is consider as OK). I'd like to check both first name and last name (i.e. two strings separated by a space) compulsory.
ohho
@ohho - updated the regex in my answer.
Amber
@Amber - it seems that the regex allows a four-part string, e.g.: `First Middle Last Fourth`, which should not be allowed. Acceptable string should be either two or three parts.
ohho
It shouldn't... the grouping only allows one contiguous set of letters (in the `[A-Za-z]+` part), and that section only gets repeated up to 3 times (`{2,3}` = 2 to 3).
Amber
Just to make sure, I've added a `^` to the beginning of the regex, perhaps you're only partial-matching the string?
Amber
The latest one works fine. Output:`|First| Bad``|First Last| Good``|Two Spaces| Good``|First Middle Last| Good``|First Middle Last Fourth| Bad``|First 123| Bad`
ohho
A: 

This is a bad idea. People may have various punctuation characters in their names, such as dashes and apostrophes. They may also have accented or foreign letters. Also, the number of middle names can vary greatly.

Adam Crume
Please focus on the _requirement_ of this question, rather than the definition of an English name ... thx
ohho