Solved

PHP Name Validation

Posted on 2007-11-25
7
3,561 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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