Solved

PHP Name Validation

Posted on 2007-11-25
7
3,562 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 500 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

726 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