0

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.

2
  • 2
    I don't think this is wise. The convention that first name and last name are separated by a space character doesn't apply to all the world's languages. Commented Aug 2, 2010 at 9:40
  • this is for a community in my local city Commented Aug 2, 2010 at 10:04

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

6 Comments

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).
Ok, but simple John does not contain any space character and it is false then.
@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.
@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: phpbin.net/x/399763185
@Sarfraz, still same results, getting error even if i typed a full name with space
|
2

What you could do is split the string then check then split string parts.

$nameSplit = explode(' ', $name);

if(count($nameSplit) < 2)
{

//Only one name given

}
else
{

  $firstName = trim($nameSplit[0]);
  $secondName = trim($nameSplit[1]);

  if($secondName == '')
  {

    //No second name given

  }

}

Comments

1

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.

2 Comments

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

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.

3 Comments

What if the user enters 'Johnandersson'? Your solution doesn't work.
An how you can recognize that it is not just a werid name ?
+1, this is actually more bulletproof (as far this can be) that the accepted solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.