Jump to content


Photo

Last name verification


  • Please log in to reply
5 replies to this topic

#1 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 11 September 2006 - 10:44 PM

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!
I'm considered good looking in some countries

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 11 September 2006 - 11:03 PM

<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>

What if there was a Smith-Howard?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 11 September 2006 - 11:10 PM

The problem with that is I dislike hyphenated names but prefer Irish people.  I umm, didn't think about that.  Nice catch.
I'm considered good looking in some countries

#4 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 11 September 2006 - 11:18 PM

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.
I'm considered good looking in some countries

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 September 2006 - 01:06 AM

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

<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>

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


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 12 September 2006 - 01:58 AM

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 =)
I'm considered good looking in some countries




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users