What is Password Strength ?

Password Strength is Checking the how much strong password.there is different method to find strength of password and method included which is missing in password like Lower Case Alphabets, Upper Case Alphabets , Numbers and special Characters.

Method 1

function password_strength($password) {
		//Level 0,1,2,3,4,5
		$Level = 0;
		if (strlen($password) > 8)
			$level++;
		//Level 2
		if (preg_match("#[A-Z]+#", $password))
			$Level++;
		//Level 3
		if (preg_match("#[a-z]+#", $password))
			$Level++;
		//Level 4
		if (preg_match("#[0-9]+#", $password))
			$Level++;
		//Level 5
		if (preg_match('/[!@#$%^&*(),+.?":{}|<>]/', $password))
			$Level++;
		//Level 6
		if (!preg_match('[ ]', $password))
			$Level++;
		return $Level;
	}

Method 2

function password_strength($password) {
		//Level 0,1,2,3,4,5
		$Level = 0;
		//Level 1
		if (strlen($password) > 8)
			$level = 1;
		//Level 2
		if (preg_match("#[A-Z]+#", $password))
			$level = 2;
		//Level 3
		if (preg_match("#[a-z]+#", $password))
			$level = 3;
		//Level 4
		if (preg_match("#[0-9]+#", $password))
			$Level = 4;
		//Level 5
		if (preg_match('/[!@#$%^&*(),+.?":{}|<>]/', $password))
			$Level = 5;
		//Level 6
		if (!preg_match('[ ]', $password))
			$Level = 6;
		return $Level;
	}
	$error = array(0 => "", 1 => "Password is Less Then 8 Words", 2 => "Upper Case Alphabets is Missing", 3 => "Lower Case Alphabets is Missing", 4 => "Numbers is Missing", 5 => "Special Characters  is Missing", 6 => "Special Characters  is Missing");
$level = password_strength("123");
echo $error[$level]; // 1
//Password is Less Then 8 Words

Method 3

function password_strength($password) {
		//Level 0,1,2,3,4,5
		$strength = array("Number" => 0, "Error" => array());
		//Level 1
		if (strlen($password) > 8) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["1"] = "Password is Less Then 8 Words";
		}
		//Level 2
		if (preg_match("#[A-Z]+#", $password)) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["2"] = "Upper Case Alphabets is Missing";
		}
		//Level 3
		if (preg_match("#[a-z]+#", $password)) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["3"] = "Lower Case Alphabets is Missing";
		}
		//Level 4
		if (preg_match("#[0-9]+#", $password)) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["4"] = "Numbers is Missing";
		}
		//Level 5
		if (preg_match('/[!@#$%^&*(),+.?":{}|<>]/', $password)) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["5"] = "Special Characters  is Missing";
		}
		//Level 6
		if (!preg_match('[ ]', $password)) {
			$strength["Number"] += 1;
		} else {
			$strength["Error"]["6"] = "Special Characters  is Missing";
		}
		$strength["Error"]["7"] = "";
		return $strength;
	}

$pass = " as";
	$strength = password_strength($pass);
	echo "<p> Level ".$strength["Number"]." </p>";
	for ($i = 0; $i <= count($strength["Error"]); $i++) {
		if (isset($strength["Error"][$i])) {
			echo "<p>Error $i : ".$strength["Error"][$i]."</p>";
		}
	}
/*
Level 1

Error 1 : Password is Less Then 8 Words
Error 2 : Upper Case Alphabets is Missing
Error 4 : Numbers is Missing
Error 5 : Special Characters  is Missing
Error 6 : Special Characters  is Missing
*/

Demo

(Visited 253 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

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