verN Posted April 10, 2007 Share Posted April 10, 2007 has anyone used a regular expression for ensuring that a name entered is valid.Where numbers ra enot alowed, sysmbols like *,)( not allowed. thanks Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 10, 2007 Share Posted April 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 10, 2007 Share Posted April 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 10, 2007 Share Posted April 10, 2007 In addition to Wildbug's comments, have a look at Unicode Properties. These will give you a larger base, and, hopefully, a cleaner looking pattern. Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 15, 2007 Share Posted April 15, 2007 Just Use \w+ Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 16, 2007 Share Posted April 16, 2007 He's asking for more than that. With \w+ a user could enter the name "______", and it would be valid. He also requested that numbers not be included. \w includes digits. Real world name matching is not easy. Quote Link to comment Share on other sites More sharing options...
c4onastick Posted April 16, 2007 Share Posted April 16, 2007 Another approach, since he has a pretty defined list of characters that are not allowed, is to use a negated class. preg_match('/^[^\d _(),*]+\z/', $text); And just explicitly disallow everything you don't want to see. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.