Check password validation in php

I made a registration validation in PHP and I'm troubleshooting each field to see if the code works to par. When I press the submit button the only part not working is the password / confirm password code block. I've been troubleshooting for hours and can't seem to find the issue.

Is possible someone can point out the issue? Thanks.


asked Mar 20, 2014 at 20:23

7

if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["cpassword"])) {
    $password = test_input($_POST["password"]);
    $cpassword = test_input($_POST["cpassword"]);
    if (strlen($_POST["password"]) <= 8) {
        $passwordErr = "Your Password Must Contain At Least 8 Characters!";
    }
    elseif(!preg_match("#[0-9]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Number!";
    }
    elseif(!preg_match("#[A-Z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!";
    }
    elseif(!preg_match("#[a-z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!";
    } else {
        $cpasswordErr = "Please Check You've Entered Or Confirmed Your Password!";
    }
}

Should be:

if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["cpassword"])) {
    $password = test_input($_POST["password"]);
    $cpassword = test_input($_POST["cpassword"]);
    if (strlen($_POST["password"]) <= '8') {
        $passwordErr = "Your Password Must Contain At Least 8 Characters!";
    }
    elseif(!preg_match("#[0-9]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Number!";
    }
    elseif(!preg_match("#[A-Z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!";
    }
    elseif(!preg_match("#[a-z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!";
    }
}
elseif(!empty($_POST["password"])) {
    $cpasswordErr = "Please Check You've Entered Or Confirmed Your Password!";
} else {
     $passwordErr = "Please enter password   ";
}

Your check for the non-matching passwords was within an if that checked to see if they matched.

answered Mar 20, 2014 at 20:26

Check password validation in php

M MillerM Miller

5,0368 gold badges39 silver badges63 bronze badges

3

Use As provided :

if(!empty($_POST["password"]) && $_POST["password"] != "" ){

    if (strlen($_POST["password"]) <= '8') {
        $err .= "Your Password Must Contain At Least 8 Digits !"."
"; } elseif(!preg_match("#[0-9]+#",$_POST["password"])) { $err .= "Your Password Must Contain At Least 1 Number !"."
"; } elseif(!preg_match("#[A-Z]+#",$_POST["password"])) { $err .= "Your Password Must Contain At Least 1 Capital Letter !"."
"; } elseif(!preg_match("#[a-z]+#",$_POST["password"])) { $err .= "Your Password Must Contain At Least 1 Lowercase Letter !"."
"; } elseif(!preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $_POST["password"])) { $err .= "Your Password Must Contain At Least 1 Special Character !"."
"; } }else{ $err .= "Please Enter your password"."
"; }

answered Oct 10, 2019 at 8:05

2

Use code below:

if(!empty($_POST["password"]) && isset( $_POST['password'] )) {
    $password = $_POST["password"];
    $cpassword = $_POST["cpassword"];
    if (mb_strlen($_POST["password"]) <= 8) {
        $passwordErr = "Your Password Must Contain At Least 8 Characters!";
    }
    elseif(!preg_match("#[0-9]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Number!";
    }
    elseif(!preg_match("#[A-Z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!";
    }
    elseif(!preg_match("#[a-z]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!";
    }
    elseif(!preg_match("#[\W]+#",$password)) {
        $passwordErr = "Your Password Must Contain At Least 1 Special Character!";
    } 
    elseif (strcmp($password, $cpassword) !== 0) {
        $passwordErr = "Passwords must match!";
    }
} else {
    $passwordErr = "Please enter password   ";
}

answered Dec 13, 2019 at 10:39

Check password validation in php

DmitryDmitry

3,5873 gold badges20 silver badges34 bronze badges

I don't think regular expressions are the best solution here. I would just have a loop with boolean variables set to false before the loop (e.g. $OneLCLetter=$OneUCLetter=$OneDigit=false;), and then set to true if a certain type of character is encountered in the loop (no problem setting a variable to true multiple times). After the loop it would be simple to go through the booleans one by one to see if any are still false.

You probably also need to check for invalid characters, such as space, tab, vertical tab, NUL etc.

answered Feb 20, 2019 at 19:54

How can I confirm my PHP password?

Just get both the password and confirm password fields in the form submit PHP and test for equality: if ($_POST["password"] === $_POST["confirm_password"]) { // success! }

How do you validate a password?

The following parameters are commonly used for password validation in any form..
Only alphanumeric inputs are accepted in the password field..
It should start with the uppercase alphabet..
At Least one uppercase alphabet password..
The password should be of a specific length..
One numeric value must be used in the password..

How do I confirm my password in HTML?

Copy Code.
.
.
Verification of valid Password .
.