How to Validate Password Strength using PHP


0

In this tutorial we will discuss how to Validate Password Strength using PHP


When the user enters their account password, it is advisable to validate the input field. Password strength validation is very helpful to check that the password is strong. The strong password makes the user account safe and helps to avoid account hacking.

Using Regex, you can smoothly validate the password strength using PHP. In the tutorial, we will explain you how to validate password strength and validate a safe password in PHP using Regex.

The given code validates the password using preg_match() function in PHP code with Regular Expression, to check that it is strong and difficult to guessing.

  • Password which user enter must be at least 8 characters in length.
  • Password which user enter must include at least one upper case letter.
  • Password which user enter must include at least one number.
  • Password which user enter must include at least one special character.

// Given password
$password = 'user-input-pass';

// Validate 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 and should include at least one upper case letter, one number, and one special character.';
}else{
    echo 'Strong password.';
}

Also read: How to Extract data Between HTML Tags in PHP


Like it? Share with your friends!

0
Developer

0 Comments