Solved

PHP Name Validation

Posted on 2007-11-25
7
3,558 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now