Reputation: 153
I want a regular expression for full name with salutation. Can anyone please help me.
^[A-Za-z] ([A-Za-z] (\\s|\\.|_)?)+[a-zA-Z]*$
this is my regular expression which I'm using for full name but it's not taking salutation.
Upvotes: 2
Views: 4387
Reputation: 168685
Rule One: Never try to enforce rules on people's names. There will always be someone who you exclude, purely because their name doesn't match what you expect.
What about people who don't have (or want) a salutation? Or those who have more than one? "Professor Sir" is a perfectly valid combination in the UK, and in Germany it's common for someone with multiple degrees to call themselves "Doctor Doctor" or something similar.
And then there's the actual names. Your regex will fail even for relatively common western-style names like "Mary-Jane O'Brien" or "André van den Berg", let alone more unusual cases.
In short, it's virtually impossible to accurately validate a name field.
Here's a link to a page which describes some of the obvious (and not so obvious) things which people try to validate on names, which can trip you up:
http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
(I've posted a similar comment before here: How to "Validate" Human Names in CakePHP?)
Upvotes: 6
Reputation: 31508
If you insist on doing this via a regexp, add (Dr|Mrs?|Ms)\.
to the pattern. Will match:
Dr.
Mr.
Mrs.
Ms.
I.e., (given that you're satisfied with the rest of the regexp - taken directly from the question.)
^(Dr|Mrs?|Ms)\. [A-Za-z] ([A-Za-z] (\s|\.|_)?)+[a-zA-Z]*$
This, however, will not be sufficient to handle Sir Nigel Oliver St. John-Mollusc III., OBE (thanks, @Tim Pietzcker).
EDIT
(Dr|Mr?s?)\.
was wrong, sorry. It would match M.
, too. Thanks, @tchrist.
Upvotes: 5
Reputation: 1219
You can use a slightly modified version of http://regexlib.com/REDetails.aspx?regexp_id=2502
Upvotes: 2