?
Solved

PHP Name Validation

Posted on 2007-11-25
7
Medium Priority
?
3,566 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
Industry Leaders: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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