[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Todd Burch

8/18/2007 4:40:00 PM

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) {
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-....

10 Answers

Todd Burch

8/18/2007 4:49:00 PM

0

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

Urban Hafner

8/18/2007 4:54:00 PM

0

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


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

> 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

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

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

Todd Burch

8/18/2007 5:17:00 PM

0

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

Stefan Rusterholz

8/18/2007 5:27:00 PM

0

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

Todd Burch

8/18/2007 5:46:00 PM

0

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

Daniel Martin

8/18/2007 5:53:00 PM

0

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) {
> 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.

--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.to_a.last )
puts "s=%q(#{s})",s.to_a.last

Alec Ross

8/18/2007 6:24:00 PM

0

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,
So no R2D2, or K-9? ;)

Alec
--
Alec Ross

Alex LeDonne

8/19/2007 12:46:00 AM

0

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.

Most assumptions about names turn out to be wrong.

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

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

Good luck!

-A

Todd Burch

8/19/2007 2:34:00 AM

0

Alex LeDonne wrote:
>
> Most assumptions about names turn out to be wrong.
>
> http://en.wikipedia.org/wiki/List_of_personal_names_that_conta...
>
> 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/...) 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-....

John Joyce

8/19/2007 4:52:00 AM

0

A great place for RegEx:
www.regexlib.com