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
https://forums.phpfreaks.com/topic/20439-last-name-verification/
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
https://forums.phpfreaks.com/topic/20439-last-name-verification/#findComment-90089
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
https://forums.phpfreaks.com/topic/20439-last-name-verification/#findComment-90152
Share on other sites

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.