How to Check Password Strength in PHP

How to check Password Strength in PHP. When the user provides their account password, it always recommended validating the input. Password strength validation is very useful to see whether the password is strong. Strong password makes the user’s account secure and helps to stop account hack.

Using Regex (Regular Expression), you’ll easily validate the password strength in PHP. Within the example code, we’ll show you ways to see password strength and validate a robust password in PHP using Regex.

The following code snippet, validate the password using preg_match() function in PHP with Regular Expression, to see whether it’s strong and difficult to guess.

  • Password must be at least 8 characters long.
  • Password must include at least one capital letter.
  • Password must include at least one number.
  • Password must include at least one special character.

here the code to check Password Strength in PHP

// 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.';
echo 'Strong password.';

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation