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.

Answer from M Miller on Stack Overflow
Top answer
1 of 4
23
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.

2 of 4
4

Use As provided :

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

    if (strlen($_POST["password"]) <= '8') {
        $err .= "Your Password Must Contain At Least 8 Digits !"."<br>";
    }
    elseif(!preg_match("#[0-9]+#",$_POST["password"])) {
        $err .= "Your Password Must Contain At Least 1 Number !"."<br>";
    }
    elseif(!preg_match("#[A-Z]+#",$_POST["password"])) {
        $err .= "Your Password Must Contain At Least 1 Capital Letter !"."<br>";
    }
    elseif(!preg_match("#[a-z]+#",$_POST["password"])) {
        $err .= "Your Password Must Contain At Least 1 Lowercase Letter !"."<br>";
    }
    elseif(!preg_match('/[\'^ยฃ$%&*()}{@#~?><>,|=_+ยฌ-]/', $_POST["password"])) {
        $err .= "Your Password Must Contain At Least 1 Special Character !"."<br>";
    }
}else{
    $err .= "Please Enter your password"."<br>";
}
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.password-verify.php
PHP: password_verify - Manual
<?php // See the password_hash() example to see where this came from. $hash = '$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.'; if (password_verify('rasmuslerdorf', $hash)) { echo 'Password is valid!'; } else { echo 'Invalid password.'; } ?> The above example will output: Password is valid!
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ how to guides โ€บ how to validate password strength in php
How to Validate Password Strength in PHP - CodexWorld
January 6, 2022 - Password strength validation - Check strong password using PHP. Use preg_match() function with Regular Expression to validate password in PHP (length 8 characters, upper case letter, number, and special character).
๐ŸŒ
Imtiaz Epu
imtiazepu.com โ€บ password-validation
Password Validation with PHP and Regular Expressions
February 4, 2023 - You may use "d" instead of "[a-z]" and "W" instead of non-word characters, symbols. You can make a manual list of most used symbols like [#.-_,$%&!]. Remember most consumers donโ€™t enjoy passwords with symbols, you can exclude emblem checks for. Just check letters, duration, caps, and numbers. $password= $_POST['password']; if (preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $password)){ echo "Your password is good."; } else { echo "Your password is bad."; }
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-password-validation
PHP Password Validation Check for Strength - PHPpot
February 13, 2024 - This password validation returns true if the entered password has at least 1 uppercase, lowercase, number and special character and with a minimum 8-character length. This is a password strength checker tool for a PHP application.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-validate-password-using-regular-expressions-in-php
How to Validate Password using Regular Expressions in PHP ? - GeeksforGeeks
January 17, 2024 - In this case, we will use basic password validation using a regular expression. ... <?php $password = "GeeksforGeeks@123"; $pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/'; if (preg_match($pattern, $password)) { echo "Valid Password"; } else { echo "Invalid Password"; } ?>
๐ŸŒ
PHPJabbers
phpjabbers.com โ€บ php-validation-and-verification-php27.html
PHP Form Validation and Verification | PHP Tutorial | PHPJabbers
$errName = ""; $errAddress = ""; $errEmail = ""; $errPassport = ""; $errPhone = ""; $errZip = ""; $errDate = ""; $errUser = ""; $errPass = ""; There are two ways to use regular expressions in PHP. One is the true PHP style in which case we have to use ereg() function and the other is to use Perl style syntax for our validations.
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_js_password_validation.asp
How To Create a Password Validation Form
Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation
Find elsewhere
๐ŸŒ
HashBangCode
hashbangcode.com โ€บ article โ€บ password-validation-class-php
Password Validation Class In PHP | #! code
The creation of random valid passwords is done through the use of the generatePassword() function. This function looks at the different values set in the object and creates a string that passes validation for those parameters.
๐ŸŒ
UI Bakery
uibakery.io โ€บ regex-library โ€บ password-regex-php
Password regex PHP
// Validate strong password $password_regex = "/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/"; echo preg_match($password_regex, 'secret'); // returns 0 echo preg_match($password_regex, '-Secr3t.'); // returns 1 ... While this regex validation is better than nothing, in situations when additional security is needed you should also check the password entered for a set of commonly used passwords like:
๐ŸŒ
Medium
medium.com โ€บ @programmerdesk.2022 โ€บ how-to-validate-strong-password-strength-in-php-152f35a8699e
How to validate strong password strength in PHP - Programmerdesk - Medium
January 10, 2024 - <form action="checker.php" method="post"> <input type="text" name="userinput"> <input type="submit" name="check"> </form> ... if(isset($_POST['submit']) && !empty($_POST['userinput'])){ $password=$_POST['userinput']; // Validating password strength $uppercase = preg_match('@[A-Z]@', $password); $lowercase = preg_match('@[a-z]@', $password); $number = preg_match('@[0-9]@', $password); $specialChars = preg_match('@[^\w]@', $password); if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) { echo 'Password should be at least 8 characters in length, should include at least one upper case letter, one number and one special character.'; }else{ echo 'Strong password.'; } }
๐ŸŒ
Clue Mediator
cluemediator.com โ€บ how-to-validate-password-strength-in-php
How to validate password strength in PHP - Clue Mediator
<!--?php $msg=""; if(isset($_POST['password'])) { $password = $_POST['password']; $number = preg_match('@[0-9]@', $password); $uppercase = preg_match('@[A-Z]@', $password); $lowercase = preg_match('@[a-z]@', $password); $specialChars = preg_match('@[^\w]@', $password);<p--> if(strlen($password) < 8 || !$number || !$uppercase || !$lowercase || !$specialChars) { $msg = "Password must be at least 8 characters in length and must contain at least one number, one upper case letter, one lower case letter and one special character."; } else { $msg = "Your password is strong."; } } ?> <title>Validate p
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Password validation - PHP - SitePoint Forums | Web Development & Design Community
May 17, 2023 - I am trying to check that a password entered contains at least 8 characters of which there should be at least 1 uppercase letter, 1 lowercase letter and 1 numeric digit. I am a numpty with regex. The pattern Iโ€™m using in my input form is ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$ but this ...
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ web development โ€บ php โ€บ php password validation
PHP password validation | Edureka Community
June 20, 2022 - I made a registration validation in PHP. But the password / confirm password code block is not working ... ?> Can someone please help me with this?
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ oop โ€บ php-oop-exercise-19.php
PHP validation class: Email, password, and field validation
May 29, 2025 - It returns true if the password is valid, and false otherwise. The "validateField($field)" method validates other common input fields. In this example, it checks if the field is not empty. It returns true if the field is valid, and false otherwise. ... Write a PHP class Validation with static methods to validate email addresses and passwords, and then test these methods with various input cases.
๐ŸŒ
GitHub
github.com โ€บ philipnorton42 โ€บ PHP-Password
GitHub - philipnorton42/PHP-Password: A password generator and validator class for PHP.
Just as an example the following password will validate to true. $password = new Password(); $password->validatePassword('qweQWE123'); The variables can be changed at runtime using various set methods.
Starred by 9 users
Forked by 10 users
Languages ย  PHP 100.0% | PHP 100.0%
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ password-and-confirm-password-validation-in-php.php
Password And Confirm Password Validation In PHP
In some forms, like registration we have to get password from user with validations. Validations like password must be greater than 8 characters, it must contain one capital, one lower, one numeric and one special character also.
๐ŸŒ
GitHub
github.com โ€บ jeremykendall โ€บ password-validator
GitHub - jeremykendall/password-validator: Validates passwords against PHP's password_hash function using PASSWORD_DEFAULT. Will rehash when needed, and will upgrade legacy passwords with the Upgrade decorator.
Validates passwords against PHP's password_hash function using PASSWORD_DEFAULT. Will rehash when needed, and will upgrade legacy passwords with the Upgrade decorator. - jeremykendall/password-validator
Starred by 142 users
Forked by 16 users
Languages ย  PHP 100.0% | PHP 100.0%
๐ŸŒ
Empty Code
emptycode.in โ€บ home โ€บ password strength validation in php
Password Strength Validation in PHP
December 25, 2023 - The first step in implementing password strength validation is to establish the criteria for a strong password. Common requirements include a minimum length, the inclusion of uppercase and lowercase letters, numbers, and special characters. With PHP, we can effortlessly define these requirements using regular expressions or string manipulation functions.