tags:

views:

52

answers:

3

Right now i have this for full name:

if(empty($_POST['full_name']) || strlen($_POST['full_name']) < 4)
{
$errormessage[] = "ERROR.\n";
}

How can i make a validation for full name, so the entered name should contain space?

So like if person enter:

John Andersson

Its ok, but if he enter:

JohnAndersson

its not ok, error. So you would need a "space" in your fullname.

+1  A: 

Like this:

if (strpos(trim($_POST['full_name']), ' ') !== false){
  // user has specified first and last name
}
elseif (strpos(trim($_POST['full_name']), ' ') !== true){
  // user has specified a single name
}

You need to enter the trim function to check for bad names such as:

  • Justin Alba[space]
  • [space]Justin Alba
  • Justin[space]
  • [space]Justin
Sarfraz
It won't work. A user can trick the server by typing a space after his single-word full name (which by the way is quite common when typing).
Anax
@Anax: Good point to downvote but see my updated answer.
Sarfraz
Ok, but simple `John` does not contain any space character and it is `false` then.
hsz
@hsz: OP says **How can i make a validation for full name, so the entered name should contain space?** not sure whether he wants single names too as he has not specified that. Updated the answer for that just in case too.
Sarfraz
@sac i cant get your code to work. It gives me error even if the person have entered correct name e.g "John Andersson".. Heres my code that i used: http://phpbin.net/x/399763185
Karem
@Karem: Modify your code like this: http://phpbin.net/x/830328087
Sarfraz
@Sarfraz, still same results, getting error even if i typed a full name with space
Karem
A: 

You can do a test like:

$fullName = 'JohnAndersson';

if ( ucfirst(strtolower($fullName)) != ucfirst($fullName) ) {
    echo 'It is not valid';
}

ucfirst(strtolower($fullName)) will convert your string to Johnandersson and it is not equals to JohnAndersson so it should contains a space.

hsz
What if the user enters 'Johnandersson'? Your solution doesn't work.
Anax
An how you can recognize that it is not just a werid name ?
hsz
+1  A: 

You could possibly have two fields, one for first name and one for surname.

I assume you don't want people with one name to fill in your form so you'd exclude Bono, Eminem, Prince, Sting, Bjork, Enya and a few others.

JW
I thought of the same argument, but then, those people all have first names and last names like everyone else that they would use when signing up. Still, I agree that insisting on a space is not a good idea.
Pekka
Yeah I suppose the contrived-named celebs would have to use their full names in legal contexts. I've just been distracted for a few minutes looking for cultures that only have one name.
JW