full name regex
Posted: Mon Jan 19, 2009 10:46 am
I just spent a few hours on this and it is driving me nuts. I just cannot get it to work. I googled and searched these forum and I am just going around in circles.
I need to do a regex on a name field using PHP. The name could be any combination of names
- John Doe
- John O'Doe
- John O' Doe
- John & Mary Doe
- John & Mary O' Doe
- John Doe-Schmoe
etc...
or of course, any combination of the above.
Here is what I tried so far:
Can anyone shed any light on this.
I need to do a regex on a name field using PHP. The name could be any combination of names
- John Doe
- John O'Doe
- John O' Doe
- John & Mary Doe
- John & Mary O' Doe
- John Doe-Schmoe
etc...
or of course, any combination of the above.
Here is what I tried so far:
Code: Select all
// doesn't work
if (!preg_match("/[^\w\s]/i",$name)) {
$this->setError('NoName');
}
// doesn't work
if (!preg_match("/[a-zA-Z -\'&]*[\s]+[a-zA-Z -\'&]*/i",$name)) {
$this->setError('NoName');
}
// doesn't work
if (preg_match("/([[:digit:]]|[~`!@#$%^*()_=+{}|\:;"/?,]|[|]|-)+//i",$name)) {
$this->setError('NoName');
}