Is this the perfect regex for validating first & last names?

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

I’m creating a form (php) and have conjoured up this regex.

My objective with this regex is to be sure I get a valid person’s name.
With that, I am assuming a real name does not contain 0-9, punctuation
chars, programming symbols, and so on. I’m allowing more than one part
names, like “Mary Beth” and “de la Cruz”, and I’m being flexible in case
the use more than one blank between names. I’ve included all the
diacritics I could find (except the a-e character that is merged - is
this still in use?)

Anyway, feedback is appreciated. Here it is:

function valid_name($name) {
return eregi("^(([a-záà âäãåçéèêëíìîïñóòôöõúùûüßÿ])+( ?)*)+$", $name) ;
}

Please excuse for the php format. Prior to the function being called,
whitespace is stripped front and back. eregi is a case insensitive
match.

A person certainly could enter a sentence or phrase (w/o punctuation, of
course) and it would be accepted as a valid name. However, that’s
beyond the scope of what I’m trying to do with the regex.

Fire away!

Thanks, Todd

Todd B. wrote:

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

Well, shucks. I missed the hyphenated last name condition. Hillary
Rodham-Clinton would get bounced… (but maybe that’s ok? LOL)

Todd B. wrote:

Todd B. wrote:

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

Well, shucks. I missed the hyphenated last name condition. Hillary
Rodham-Clinton would get bounced… (but maybe that’s ok? LOL)

Revised to allow hyphens:

“^(([a-záà âäãåçéèêëíìîïñóòôöõúùûüßÿ])+(-?|( ?)*))+$”

Todd B. wrote:

Revised to allow hyphens:

“^(([a-záà âäãåçéèêëíìîïñóòôöõúùûüßÿ])+(-?|( ?)*))+$”

Well, so much for testing my own code. “zá” fails. All the diacritics
fails.

Back to the drawing board.

Urban H. wrote:

Are you really sure you want to ask a PHP question in a Ruby forum? I
guess you’ll get a better answer in a PHP forum.

Actually, it’s a regex question. Lots of regex questions and answers
get posted here. Lots of regex expertise here. Is there a regex forum
somewhere else I should be using? I could have dummied this up to
look like it was in ruby, or, not even mentioned php I guess.

Your “other” character example did not show up.

Thanks, Todd

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Aug 18, 2007, at 19:39 , Todd B. wrote:

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

I’m creating a form (php) and have conjoured up this regex.

Are you really sure you want to ask a PHP question in a Ruby forum? I
guess you’ll get a better answer in a PHP forum.

My objective with this regex is to be sure I get a valid person’s
name.
With that, I am assuming a real name does not contain 0-9, punctuation
chars, programming symbols, and so on. I’m allowing more than one
part
names, like “Mary Beth” and “de la Cruz”, and I’m being flexible in
case
the use more than one blank between names. I’ve included all the
diacritics I could find (except the a-e character that is merged - is
this still in use?)

Sure it is (in Danish an Norwegian). And there’s the ø character. Oh and
those (and a few others) could also be upper case …

Urban

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)

iD8DBQFGxyQXggNuVCIrEyURAsT1AJ4nKS/W6CXvGx4sCVjfLo0dopJuFQCfSPjd
AZvI8VsCdYT/2ZmxgTNO904=
=Tq3q
-----END PGP SIGNATURE-----

Stefan R. wrote:

Todd B. wrote:

Just that ereg* is not pcre. There is no equivalent in ruby to the ereg*
functions.

Regards
Stefan

Ok then. I’ll find the proper resource. Thanks.

Todd B. wrote:

Urban H. wrote:

Are you really sure you want to ask a PHP question in a Ruby forum? I
guess you’ll get a better answer in a PHP forum.

Actually, it’s a regex question. Lots of regex questions and answers
get posted here. Lots of regex expertise here. Is there a regex forum
somewhere else I should be using? I could have dummied this up to
look like it was in ruby, or, not even mentioned php I guess.

Your “other” character example did not show up.

Thanks, Todd

Just that ereg* is not pcre. There is no equivalent in ruby to the ereg*
functions.

Regards
Stefan

Todd B. [email protected] writes:

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

I’m creating a form (php) and have conjoured up this regex.

My objective with this regex is to be sure I get a valid person’s name.
With that, I am assuming a real name does not contain 0-9, punctuation
chars, programming symbols, and so on.

Anyway, feedback is appreciated. Here it is:

function valid_name($name) {
return eregi("^(([a-záàâäãåçéèêëíìîïñóòôöõúùûüßÿ])+( ?)*)+$", $name) ;
}

I don’t know PHP regular expressions, but a bit of googling suggests
that you can use standard POSIX character classes, so I’d replace that
list of letters with [[:alpha:]]. You also have some extraneous
parentheses that could be stripped away, so that leaves you with:

return eregi("^([[:alpha:]]+(-| +)?)+$", $name) ;

That adds in the hyphenated name thing, but disallows something like:
Hillary Rodham–Clinton
or
Hillary Rodham- Clinton
You’re allowed either some number of spaces or a single dash between
words, but not multiple dashes or both a dash and a space.

At this point, if it isn’t working you may be stuck in locale and/or
character set encoding issues. Working those out is left as an
exercise for the one of us closer to the PHP install. A word of
warning: i18n can drive you positively batty if you let it.

In message [email protected], Todd B.
[email protected] writes

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

I’m creating a form (php) and have conjoured up this regex.

My objective with this regex is to be sure I get a valid person’s name.
With that, I am assuming a real name does not contain 0-9,
So no R2D2, or K-9? :wink:

Alec

On 8/18/07, Todd B. [email protected] wrote:

Perhaps a fairly bold statement, coming from a novice regex’er. :slight_smile:

I’m creating a form (php) and have conjoured up this regex.

My objective with this regex is to be sure I get a valid person’s name.
With that, I am assuming a real name does not contain 0-9, punctuation
chars, programming symbols, and so on.

Most assumptions about names turn out to be wrong.

http://en.wikipedia.org/wiki/List_of_personal_names_that_contain_numbers

Note names like Jennifer 8. Lee, Perri 6, Nancy 3. Hoffman.

Good luck!

-A

Alex LeDonne wrote:

Most assumptions about names turn out to be wrong.

http://en.wikipedia.org/wiki/List_of_personal_names_that_contain_numbers

Note names like Jennifer 8. Lee, Perri 6, Nancy 3. Hoffman.

Good luck!

-A

HA HA. Seeing as there are so few, I think I’ll copy those names into
an array and specifically reject them. LOL. I joked around with some
buddies back in the mid 80’s about prefixing my first kid’s name with
[email protected]”, as the code we were working on at the time contained a lot of
modules with this prefix. We got a good laugh out of it. Never did do
it though - the wife (http://imdb.com/title/tt0092746/) wouldn’t have
gone for it.

I worked the regex out to my satisfaction. I was taking the wrong
approach. I decided to reject invalid characters, or what I consider to
be invalid characters for a human name on our planet, instead of only
accepting valid characters. Shorter, simpler, and it met my other goal
of being one facet of stopping a potential spam injection as well.

Todd

A great place for RegEx:
www.regexlib.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs