Jump to content

Last name verification


gijew

Recommended Posts

Hi,

I'm in a posting mood today so I might as well post this as well = )

I have this tiny regex guy here that (as you may see) only checks to ensure the last name has letters ONLY.  I got to thinking about that and realized that there are a few O'Brians or those Irish people that like to mess me up from time to time.  Anyhoo, I started trying to play around with it only to realize that I was getting nowhere.

If (!preg_match('/^[a-zA-Z]+$/i', $_POST['PreQualification_First_Name'])) {
  // fail
)

I tried escaping the ' but I'm guessing there is more to it.  I just want the sucker to not fail for the Irish people of the world = )

Thank you!
Link to comment
Share on other sites

I played around with what you had a bit and started thinking some more.  I'm going to throw it out but I don't know how much time you're willing to invest in this = )

<?php
$tests = array(
                    'Smith',
                    'Howard',
                    'Conan-Neil',
                    'Von Dutchman',
                    'Last, First',
                    'First Middle Last',
                    'Jason2',
                    "O'Brian",
                    );

foreach ($tests as $test) {
  echo $test, ' => ';
  echo preg_match('/^(?:O\')?[A-Za-z -]+$/', $test) ? 'OK' : 'Not OK' ;
  echo '<br/>';
}
?>

So all I did was throw a hypen and a space in there.  I started to think of the different name types out there.  The only name I know of that is non-Klingon that has an apostrophe is like O'Brian, O'Keefe, etc.  There are names like Von Dusch or whatever.  So basically can you help me limit the amount of spaces in a string to one and maybe only allow the letter "O" to follow the apostrophe?  I don't know if that's too far out of the scope here or if I'm wrong on the naming convention.
Link to comment
Share on other sites

I'm assuming 2 letters is the shortest last name.

[code]
<pre>
<?php

$tests = array(
'Smith',
'Howard',
'Conan-Neil',
'Von Dutchman',
'Last, First',
'First Middle Last',
'Jason2',
"O'Brian",
'a',
'ab',
'abc',
'a-b',
'ab-a',
'ab-ab',
'a b',
'ab a',
'ab cd',
);

foreach ($tests as $test) {
echo $test, ' => ';
echo preg_match('/
^ ### BOL
(?:O\')? ### Possible Oapostrophe.
[A-Za-z]{2,} ### At least 2 letters.
([\s-])? ### Possible (white)space or hyphen.
### Require at least 2 more letters if a space or hyphen was found;
### otherwise, require any amount, even 0.
(?(1)[A-Za-z]{2,}|[A-Za-z]*)
$ ### EOL
/x', $test) ?
'OK' :
'Not OK' ;
echo '<br/>';
}

?>
</pre>
[/code]

[quote]
Smith => OK
Howard => OK
Conan-Neil => OK
Von Dutchman => OK
Last, First => Not OK
First Middle Last => Not OK
Jason2 => Not OK
O'Brian => OK
a => Not OK
ab => OK
abc => OK
a-b => Not OK
ab-a => Not OK
ab-ab => OK
a b => Not OK
ab a => Not OK
ab cd => OK
[/quote]
Link to comment
Share on other sites

Everytime you've helped me you always make it way above my head so I have to learn what the hell you did.  Thanks alot for teaching me.  I don't remember my kids names now.

Oh yeah...thanks for the help.  It is very much appreciated =)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.