tags:

views:

11

answers:

3

Actually I have done a regexp for this, in order to avoid fake names (at least not with some weird symbols). I'm doing this in PHP. The regexp is:

/ [ \ ^ < , \ " @ \ / \ { \ } \ ( \ ) \ * \ $ % \ ? = > : \ | ; # ] + /

But it's failing and I don't know why (just don't detect the symbols). All I want is, if some of those symbols (let's say, @ # * / \ for instance), return false (like every other regexp).

Thank you very much in advance!

PS: Sorry, I totally forgot it. I want all type of names: with ´, ¨, ñ, including but not exclusive japanese names. Also name or surname could include spaces!

+1  A: 

^[A-Za-z, .]+$

Only allow A through Z, space, period, and comma. A through Z are obvious. Period and comma are needed for titles (ie: Dougie Howser, M.D.).

Joel Coehoorn
Would this accept any kind of names, i.e. ´, ¨, japanese, ...?
No, but it's easy to add the accent characters to the allowed list. Trust me on this: maintaining an allowed list is much simpler than maintaining a dis-allowed list.
Joel Coehoorn
But what about a japanese person, or chinese, who wants to register with kanjis? Also, I need to add every `, ´, ¨, ^... to the list?
If you want to allow kanji, you're better off just not validating the name. I mean that.
Joel Coehoorn
Anyway, Japanese people would be for a very far future (not now), so I would be nice knowing how to add symbols like ´, `, ¨, ^ (accents at all) to this expression.
You really should watch Tony the Pony + Jon Skeets presentation "Humanity: EPIC FAIL". Then again, this even fails with "western" hyphenated names.
Daniel Beck
A: 

You could do it the other way, and have it return true if it only contains letters, no numbers or special characters.

Like this I believe (sorry, been quite a long time since I've done regex): m/^[a-zA-Z]+$/

Mistiry
But the question was about names of human beings. Very few people have a name that is normally written with only latin letters.
Gilles
Your best bet, then, is to NOT validate the name. Validate other fields, but if you want to allow every name from every culture, every symbol used in names...you're better off skipping validation on the name.
Mistiry
+2  A: 

I haven't looked at your regexp (you screwed up the formatting, which doesn't help), but I'm willing to bet that it's wrong. (Actually, I think you've got commas as forbidden characters. There, I've won my bet. Commas are rare, but routine, in USAnian names.)

Read Patrick McKenzie's article “Falsehoods Programmers Believe About Names”. If you still want to use a regexp after this, clearly you haven't read the article, so go and read it.

Remember, every time you reject a name as invalid, someone has such a name vows to kill your children, and their grandchildren unto the seventh generation.This has been said so well before that I can but cite the Truth (just read “names” where is says “HTML”). You cannot validate names with regexps. Even Perl's enhanced irregular expressions cannot validate names. Even John T cannot validate names using regular expressions. The cen'ter cannot hold it is too late. 名称無効なसर्वर दोषהצפת מחסנית

Don't use regexps to validate names! Think of the children!

-- Gilles "Bobby" O'Malley-McDonald--U, Jr.

Gilles
True - but if you simply want to check that they haven't entered their email address or phone number in the 'name field'
Martin Beckett