PHP error while validating username

I am creating a site using PHP and I need to check if the user entered name is correct or not. Since JavaScript is client-side I can't completely rely on this, so here's my server-side function validating the username:

function validate_name($name) {
    $name = trim($name);  // only for the purpose of debugging <---- edited comment
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return true;
    }
}

      

After that I check the result of the input and display respectively:

function final_check() {
    if (validate_name($_POST["first_name"]) == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

      

When I don't enter anything in the field first_name

, I get a blank error message; when i enter numbers i get numbers error. However, when I enter a valid name, it gives me a blank error message.

Here's the post data:

Array
(
    [email] => ewf@gmail.com
    [first_name] => qwe
    [last_name] => wqe
    [password] => qwe
    [re_password] => qwe
    [gender] => Male
)

      

Output:

Your first name cannot be empty

      

Any idea what I am doing wrong? I've been stumped for the last hour trying to fix this and I haven't been able to find a solution.

+3


source to share


5 answers


Array
(
    [email] => ewf@gmail.com
    [first_name] => qwe
    [last_name] => wqe
    [password] => qwe
    [re_password] => qwe
    [gender] => Male
)

      

// Start_name = 3

function validate_name($name) {
    $name = trim($name);
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return true;      // satisfy this case return true
    }
}

      

and here it becomes like



function final_check() {
    if (validate_name($_POST["first_name"]) == "small") {   // if(1 == 'small')
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

      

if (1 == 'small') is a Boolean string comparison that always returns true.

Please check this page in the manual to understand the problem.

+8


source


try it

if (validate_name($_POST["first_name"]) == "small" && !validate_name($_POST["first_name"])) {
    echo "<span class='error'>Your first name cannot be empty</span>";
    return false;
} else if (validate_name($_POST["first_name"]) == "numbers" && validate_name($_POST["first_name"])) {
    echo "<span class='error'>Numbers are not allowed in your first name</span>";
    return false;
}

      

true == 'small'

will also return true



or try with ===

will strictly match the values

if (validate_name($_POST["first_name"]) === "small") {
    echo "<span class='error'>Your first name cannot be empty</span>";
    return false;
} else if (validate_name($_POST["first_name"]) === "numbers"=) {
    echo "<span class='error'>Numbers are not allowed in your first name</span>";
    return false;
}

      

+1


source


Your logic is correct, however, as other questions have pointed out, a boolean comparison string will return true.

To fix this, you should use ===

instead ==

and everything should be fine

+1


source


What I suggest here is to change your function validate_name

so that it will only have one return type since it can now return either string

or boolean

, which is not good practice.

For example, you can do something like:

function validate_name($name) {
    $name = trim($name);
    echo $name;
    if (strlen($name) <= 1) {
        return "small";
    } else if (has_numbers($name)) { 
        return "numbers";
    } else {
        return "ok"; // or return ""; or whatever string you want.
    }
}

      

Also, to optimize your code and improve readability, you can change your function final_check

to validate_name

only have one call using an intermediate option.

function final_check() {
    $valid = validate_name($_POST["first_name"]);
    if ($valid == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if ($valid == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
    return true;
}

      

+1


source


I think you only need to change the final_check () function

function final_check() {
if(validate_name($_POST["first_name"])){
 return true;
}else{
    if (validate_name($_POST["first_name"]) == "small") {
        echo "<span class='error'>Your first name cannot be empty</span>";
        return false;
    } else if (validate_name($_POST["first_name"]) == "numbers") {
        echo "<span class='error'>Numbers are not allowed in your first name</span>";
        return false;
    }
  }
}

      

-1


source







All Articles