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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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