Jump to content

Archived

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

gijew

Last name verification

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!

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

$tests = array(
'Smith',
'Howard',
"O'Brian",
);

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

?>
</pre>
[/code]

What if there was a Smith-Howard?

Share this post


Link to post
Share on other sites
The problem with that is I dislike hyphenated names but prefer Irish people.  I umm, didn't think about that.  Nice catch.

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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 =)

Share this post


Link to post
Share on other sites

×

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.