Click to See Complete Forum and Search --> : How to require two names in an input field?


Moldarin
08-17-2006, 06:55 PM
Hi,

How can I modify my WordPress powered Website's (http://thewebdesignjournal.comm/journal/) name field in the comment section to require two names?

I want this feature to ensure that users type in both their first and their surname.

NogDog
08-17-2006, 08:09 PM
The obvious solution would be to make it into two required fields. If that's not practical for some reason, then you'd probably have to use some sort of regex check:

$name = trim($_POST['name']);
if(preg_match('/^[a-z]+(\s+[a-z]+)+$/i', $name))
{
// at least two names provided
}
else
{
// invalid
}

You'd have to decide how strict/loose you'll be and what characters you'd allow. For instance, the above would not allow "Charles W. Reace, Jr." nor "Kevin O'Connell", due to the punctuation marks. (Thus my recommendation to simply make it two fields, making everyone's life simpler.)