Jump to content

Preg_match() RegEx for First Name and Last Name in Separate Fields


OldWest

Recommended Posts

I've been cracking at this for the part of 2 days now, and I cannot get my regex to work properly in my script.

 

/^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i

 

The above is for a last name field, and it needs to be able to accept: Roberts, Jr. (as an example)...

 

As you can see in my regex, I am allowing all of these characters.. And it's still not validating it.

 

You can test to see what I mean here:

 

<?php 

$last_name = "Roberts, Jr.";

if (!preg_match("/^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i", $last_name )) {
          echo "Sorry no dice!";
      }

?>

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.