How to validate strong password strength in PHP

This tutorial we are validate user password. that require while registering user on system. PHP provide the preg_match function. So we can use this function to implement this.

We will check at least one upper case, One Lower case letter with numeric and special character.

<?php
// User input password.
$password = 'Aa@!navdata1';

// 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.';
}
?>

Run this code on editor, it will display strong password. as we already input user password.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply