Jump to content

[SOLVED] reg for names


verN

Recommended Posts

I guess it would be something like [A-Za-z'-]{2,}.  That assumes a name consists only of letters and possible hyphens or apostrophes, and is at least two characters in length.  Of course, it's not restrictive enough because input like '-'-'-'-'-'-' will match.  It also might be too restrictive in excluding characters in the extended ASCII "alphabet," like ä/ô/ú, etc.  To make a closer-to-reality pattern while still being restrictive, I suspect you would have to define a character class with letters, extended/accented letters and create a pattern which allows for the possibility of a single hyphen or apostrophe in certain places.  You should take a look at alot of names, including non-English names to get an idea of what the pattern should include or exclude.

Link to comment
https://forums.phpfreaks.com/topic/46431-solved-reg-for-names/#findComment-225858
Share on other sites

I did a little thinking about this and came up with a character class and a fairly long pattern.  It may not be restrictive enough 100% of the time, it might need some tweaking, and it looks a little ridiculous, but try it out as a starting point if you want.

 

The character class containing letters and extended characters: 
[A-Za-z\128-\151\153\154\160-\165]

The pattern:
/^(?:[A-Za-z\128-\151\153\154\160-\165]*'? ?[A-Za-z\128-\151\153\154\160-\165]+[- ]?[A-Za-z\128-\151\153\154\160-\165]+ ?){1,3}$/

 

I also added some possible spaces in this pattern.  A possible beginning or trailing apostrophe with a possible space thereafter, at least one letter, a possible hyphen or space break, at least another letter, and the possibility that the whole thing is repeated.

 

I could have used the \w class as it is locale sensitive, except it also matches the underscore character.

Link to comment
https://forums.phpfreaks.com/topic/46431-solved-reg-for-names/#findComment-225892
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.