Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Name Validation

Posted on 2007-11-25
7
Medium Priority
?
3,568 Views
Last Modified: 2010-04-21
I need help creating a regex to validate either a first name or a last name (separate fields on my form).  Specifically:

1) The name must contain only letters, one space, a single apostrophe, or a single hyphen, or a single period;
2) If the name contains either an apostrophe, a hyphen, or a space, the apostrophe, hyphen, or space must be preceded and followed by at least one letter.
3) If the name contains a period, it must be preceded by a letter and followed by a space and letter.

Thanks.

Alan
0
Comment
Question by:alanpollenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20348074
I think this'll do what you need...
^\w+((([ '-])|(\.[ ]))\w+)?$

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 20348083
This version is a little better as it doesn't do any capturing: -


^\w+(?:(?:(?:[ '-])|(?:\.[ ]))\w+)?$
 
Analysis: -
 
^ (anchor to start of string)
Any word character 
+ (one or more times)
Non-capturing Group
  Non-capturing Group
    Non-capturing Group
      Any character in " '-"
    End Capture
        or
    Non-capturing Group
      .
      Any character in " "
    End Capture
  End Capture
  Any word character 
  + (one or more times)
End Capture
? (zero or one time)
$ (anchor to end of string)

Open in new window

0
 
LVL 9

Expert Comment

by:waygood
ID: 20349501
Just an observation, but there is a contradiction in your rules.

2. A space must be preceded and followed by at least one letter.
 eg  A_A  (_ is a space)
3. A period (non letter) must be preceeded by a letter and be followed by a space and a letter.
 eg A._A (_ is a space)

Since a space must be preceeded by a letter (not a period) all examples for rule 3 will be wrong for rule 2
I assume rule 2 should be "A space must be preceded and followed by at least one character"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:alanpollenz
ID: 31410915
Thanks!  And waygood was correct regarding rules 2 and 3.
0
 

Author Comment

by:alanpollenz
ID: 20350042
One thing I did notice was that when I put this into a function such as:

---------------------------------------------------------------------------------------

function allowed_name ($string) {
      $pattern="/^\w+(?:(?:(?:[ '-])|(?:\.[ ]))\w+)?$/";
      preg_match($pattern, stripslashes($string), $results); // any matches found will be arryed into results
      if ( '' <> trim(' ' . @$results[0]) ) {
            return true;
            }
      else {
            return false;
            }
      }

---------------------------------------------------------------------------------------

I had to use the stripslashes function to allow the apostrophe because in a name like O'Reilly it was passed as O\'Reilly and the match failed.

Alan
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20350048
Sorry -- I know not of PHP, just regexes :)
0
 

Expert Comment

by:jnichols1
ID: 21525811
This is what I use the regular expression allows apostrophe's, underscores, a-z, 0-9, periods and splits at the @ symbol and allows two periods after it i.e. tool'omally@tool.co.uk or todd_head@tool.com

//validate the email
function validate_email($email)
{
// regexp for a valid email address
$regexp = "^(['_a-z0-9-]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$";

return (eregi($regexp, $email) > 0);
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question