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

**URL:** <https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028>\
**Category:** ruby-talk\
**Created:** [18 August 2007 16:39 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028 "2007-08-18T16:39:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 16:39 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/1 "2007-08-18T16:39:43Z")

</div>

Perhaps a fairly bold statement, coming from a novice regex'er. 🙂

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) {  
&nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 16:48 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/2 "2007-08-18T16:48:54Z")

</div>

Todd Burch wrote:

> Perhaps a fairly bold statement, coming from a novice regex'er. 🙂

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Urban\_Hafner2](https://avatars.discourse-cdn.com/v4/letter/u/9fc348/32.png) [@Urban\_Hafner2](https://rubytalk.org/u/Urban_Hafner2)\
**Post date:** [18 August 2007 16:53 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/3 "2007-08-18T16:53:49Z")

</div>

> Perhaps a fairly bold statement, coming from a novice regex'er. 🙂
> 
> 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

> **···**
>
> On Aug 18, 2007, at 19:39 , Todd Burch wrote:

---

<div class="post-metadata">

**Author:** ![Daniel\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Daniel\_Martin](https://rubytalk.org/u/Daniel_Martin)\
**Post date:** [18 August 2007 17:52 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/4 "2007-08-18T17:52:56Z")

</div>

Todd Burch \<promos@burchwoodusa.com\> writes:

> Perhaps a fairly bold statement, coming from a novice regex'er. 🙂
> 
> 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.

\<snip\>

> Anyway, feedback is appreciated. Here it is:
> 
> function valid\_name($name) {  
> &nbsp;&nbsp;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:

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

That adds in the hyphenated name thing, but disallows something like:  
&nbsp;&nbsp;&nbsp;Hillary Rodham--Clinton  
or  
&nbsp;&nbsp;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.

> **···**
>
> --  
> s=%q( Daniel Martin -- martin@snowplow.org  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "s=%q(#{s})",s.to\_a.last )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "s=%q(#{s})",s.to\_a.last

---

<div class="post-metadata">

**Author:** ![Alec\_Ross](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Alec\_Ross](https://rubytalk.org/u/Alec_Ross)\
**Post date:** [18 August 2007 18:30 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/5 "2007-08-18T18:30:08Z")

</div>

So no R2D2, or K-9? 😉

Alec

> **···**
>
> In message \<761c72b492471cd116292b3af045a164@ruby-forum.com\>, Todd Burch \<promos@burchwoodusa.com\> writes
> 
> > Perhaps a fairly bold statement, coming from a novice regex'er. 🙂
> > 
> > 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,
> 
> --  
> Alec Ross

---

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://rubytalk.org/u/A_LeDonne)\
**Post date:** [19 August 2007 00:46 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/6 "2007-08-19T00:46:12Z")

</div>

Most assumptions about names turn out to be wrong.

[http://en.wikipedia.org/wiki/List\_of\_personal\_names\_that\_contain\_numbers](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

> **···**
>
> On 8/18/07, Todd Burch \<promos@burchwoodusa.com\> wrote:
> 
> > Perhaps a fairly bold statement, coming from a novice regex'er. 🙂
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 16:39 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/7 "2007-08-18T16:39:41Z")

</div>

Todd Burch wrote:

> Todd Burch wrote:
> 
> > Perhaps a fairly bold statement, coming from a novice regex'er. 🙂
> 
> 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áàâäãåçéèêëíìîïñóòôöõúùûüßÿ])+(\-?|( ?)\*))+$"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 16:51 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/8 "2007-08-18T16:51:54Z")

</div>

Todd Burch 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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 17:17 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/9 "2007-08-18T17:17:26Z")

</div>

Urban Hafner 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [19 August 2007 02:34 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/10 "2007-08-19T02:34:23Z")

</div>

Alex LeDonne wrote:

> Most assumptions about names turn out to be wrong.
> 
> [http://en.wikipedia.org/wiki/List\_of\_personal\_names\_that\_contain\_numbers](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  
"DSN@", 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/\](http://imdb.com/title/tt0092746/%5C)) 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [18 August 2007 17:27 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/11 "2007-08-18T17:27:25Z")

</div>

Todd Burch wrote:

> Urban Hafner 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [19 August 2007 04:51 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/12 "2007-08-19T04:51:30Z")

</div>

A great place for RegEx:  
[www.regexlib.com](http://www.regexlib.com)

---

<div class="post-metadata">

**Author:** ![Todd\_Burch](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@Todd\_Burch](https://rubytalk.org/u/Todd_Burch)\
**Post date:** [18 August 2007 17:46 UTC](https://rubytalk.org/t/is-this-the-perfect-regex-for-validating-first-last-names/40028/13 "2007-08-18T17:46:10Z")

</div>

Stefan Rusterholz wrote:

> Todd Burch 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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
