Scroll to top
© 2020, Empty Code | All Rights Reserved
Share

Password Strength Validation in PHP


Rahul Kumar Sharma - September 26, 2019 - 0 comments

Validation Password Strength is very recommended to help users to find a secure password. Secure Password will help to secure accounts from attackers. In PHP we can Validate Password Strength using preg_match() function.

The recommended password must have:

  1. The password must be at least 8 characters in length.
  2. The password must include at least one upper case.
  3. The password must include at least one number.
  4. The password must include at least one special character.

HTML

<form method='POST' action="">
  Password : <input type="text" name="password">
  <input type="submit" name="submit">
</form>

PHP

<?php
if isset($_POST['submit']){
  $password = $_POST['password'];
  
  // 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 is not Strong';
  }
  else{
    echo 'Password is Strong';
  } 
}
?>

Related posts

Post a Comment

Your email address will not be published. Required fields are marked *