Skip to content

Instantly share code, notes, and snippets.

@madan712
Created September 14, 2013 08:03
Show Gist options
  • Save madan712/6559819 to your computer and use it in GitHub Desktop.
Save madan712/6559819 to your computer and use it in GitHub Desktop.
Javascript - Password strength meter
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Password strength meter </title>
<script language="javascript">
var m_strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var m_strLowerCase = "abcdefghijklmnopqrstuvwxyz";
var m_strNumber = "0123456789";
var m_strCharacters = "!@#$%^&*?_~";
function checkPwdStrength() {
var pwd = document.getElementById("pwd").value;
len = pwd.length;
var scorebar = document.getElementById('scorebar');
var comp = "";
if (len == 0) {
scorebar.style.backgroundPosition = '0px 0px';
comp = "Password Strength";
}
else {
scr = parseInt(getPwdScore(pwd));
if(scr >= 90) {
scorebar.style.backgroundPosition = '0px -30px';
comp = "Very Strong";
}
else if(scr >= 70) {
scorebar.style.backgroundPosition = '0px -24px';
comp = "Strong";
}
else if(scr >= 50) {
scorebar.style.backgroundPosition = '0px -18px';
comp = "Good";
}
else if(scr >= 30) {
scorebar.style.backgroundPosition = '0px -12px';
comp = "Weak";
}
else if(scr >= 0) {
scorebar.style.backgroundPosition = '0px -6px';
comp = "Very Weak";
}
}
document.getElementById('complexity').innerHTML = comp;
return false;
}
function getPwdScore(strPassword) {
// Reset combination count
var nScore = 0;
// Password length
// -- Less than 4 characters
if (strPassword.length < 5) {
nScore += 5;
}
// -- 5 to 7 characters
else if (strPassword.length > 4 && strPassword.length < 8) {
nScore += 10;
}
// -- 8 or more
else if (strPassword.length > 7) {
nScore += 25;
}
// Letters
var nUpperCount = countContain(strPassword, m_strUpperCase);
var nLowerCount = countContain(strPassword, m_strLowerCase);
var nLowerUpperCount = nUpperCount + nLowerCount;
// -- Letters are all lower case
if (nUpperCount == 0 && nLowerCount != 0) {
nScore += 10;
}
// -- Letters are upper case and lower case
else if (nUpperCount != 0 && nLowerCount != 0) {
nScore += 20;
}
// Numbers
var nNumberCount = countContain(strPassword, m_strNumber);
// -- 1 number
if (nNumberCount == 1) {
nScore += 10;
}
// -- 3 or more numbers
if (nNumberCount >= 3) {
nScore += 20;
}
// Characters
var nCharacterCount = countContain(strPassword, m_strCharacters);
// -- 1 character
if (nCharacterCount == 1) {
nScore += 10;
}
// -- More than 1 character
if (nCharacterCount > 1) {
nScore += 25;
}
// Bonus
// -- Letters and numbers
if (nNumberCount != 0 && nLowerUpperCount != 0) {
nScore += 2;
}
// -- Letters, numbers, and characters
if (nNumberCount != 0 && nLowerUpperCount != 0 && nCharacterCount != 0) {
nScore += 3;
}
// -- Mixed case letters, numbers, and characters
if (nNumberCount != 0 && nUpperCount != 0 && nLowerCount != 0
&& nCharacterCount != 0) {
nScore += 5;
}
return nScore;
}
// Checks a string for a list of characters
function countContain(strPassword, strCheck) {
// Declare variables
var nCount = 0;
for (i = 0; i < strPassword.length; i++) {
if (strCheck.indexOf(strPassword.charAt(i)) > -1) {
nCount++;
}
}
return nCount;
}
</script>
<style type="text/css">
#pmId {
}
#scorebarBorder {
border: 1px solid #666666;
height: 5px;
position: relative;
width: 122px;
}
#scorebar {
background-image: url("pm.png");
}
#scorebar {
background-position: 0 0;
background-repeat: no-repeat;
height: 5px;
width: 122px;
z-index: 0;
}
#complexity {
padding: 0;
text-align: center;
top: 0;
width: 122px;
z-index: 10;
}
</style>
</head>
<body>
<table border="0" align="center">
<tr>
<td><input type="password" id="pwd" value="" onkeyup="return checkPwdStrength();" /></td>
<td>
<div id="pmId">
<div id="scorebarBorder">
<div id="scorebar" style="background-position: 0px 0px;"></div>
</div>
<div id="complexity">
Password Strength
</div>
</div>
</td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment