This script measures the strength of a password. It's easy to integrate it into a registration form or whatever. This demo has 2 files.
password.js
demo.html [this is a DEMO of how it works]
EXPLANATION
In password.js
First row -> forbidden strings for password (u can add as many you want)
Rows 2,3,4,5 -> vars containing character sets (you can add or erase from existing ones. U also must add the IF sequence for ones you add or remove it if u remove a containing var)
In demo.html
Integrate
into a registration form
Inspired from : https://www.microsoft.com/protect/yourself/password/checker.mspx and
http://www.codeandcoffee.com/2007/06/27/how-to-make-a-password-strength-meter-like-google/
Last edited by gidevelop on Wed Jan 09, 2008 1:27 pm; edited 1 time in total
password.js
| Code: |
| var commonPasswords = new Array('password', 'pass', '1234', '1246');
var numbers = "0123456789"; var punctuation = "!.@$£#*()%~<>{}[]"; var lowercase = "abcdefghijklmnopqrstuvwxyz"; var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; function checkPassword(password) { var combinations = 0; if (contains(password, numbers) > 0) { combinations += 10; } if (contains(password, lowercase) > 0) { combinations += 26; } if (contains(password, uppercase) > 0) { combinations += 26; } if (contains(password, punctuation) > 0) { combinations += punctuation.length; } var totalCombinations = Math.pow(combinations, password.length); if (isCommonPassword(password)) { totalCombinations = 75000 } var timeInSeconds = (totalCombinations / 200) / 2; var timeInDays = timeInSeconds / 86400 var lifetime = 365; var percentage = timeInDays / lifetime; var friendlyPercentage = cap(Math.round(percentage * 100), 100); if (totalCombinations != 75000 && friendlyPercentage < (password.length * 5)) { friendlyPercentage += password.length * 5; } var progressBar = document.getElementById("progressBar"); progressBar.style.width = friendlyPercentage + "%"; if (percentage > 1) { progressBar.style.backgroundColor = "#3bce08"; return; } if (percentage > 0.5) { progressBar.style.backgroundColor = "#ffd801"; return; } if (percentage > 0.10) { progressBar.style.backgroundColor = "orange"; return; } if (percentage <= 0.10) { progressBar.style.backgroundColor = "red"; return; } } function cap(number, max) { if (number > max) { return max; } else { return number; } } function isCommonPassword(password) { for (i = 0; i < commonPasswords.length; i++) { var commonPassword = commonPasswords[i]; if (password == commonPassword) { return true; } } return false; } function contains(password, validChars) { count = 0; for (i = 0; i < password.length; i++) { var char = password.charAt(i); if (validChars.indexOf(char) > -1) { count++; } } return count; } |
demo.html [this is a DEMO of how it works]
| Code: |
| <html>
<head> <title>Javascript Password Strength Validator</title> </head> <body> <h1>Javascript - Pass strength [DEMO FILE]</h1> <script type="text/javascript" src="password.js"></script> <table> <tr> <td>Parola:</td> <td><input type="text" onKeyUp="checkPassword(this.value)"/></td> <td> <div style="border: 1px solid black; width: 100px;"> <div id="progressBar" style="font-size: 1px; height: 20px; width: 0px; border: 1px solid white;"> </div> </div> </td> </tr> </table> <p>Credit: GIDevelop.frih.net</p> </body> </html> |
EXPLANATION
In password.js
First row -> forbidden strings for password (u can add as many you want)
Rows 2,3,4,5 -> vars containing character sets (you can add or erase from existing ones. U also must add the IF sequence for ones you add or remove it if u remove a containing var)
In demo.html
Integrate
| Code: |
| <td>Parola:</td>
<td><input type="text" onKeyUp="checkPassword(this.value)"/></td> <td> <div style="border: 1px solid gray; width: 100px;"> <div id="progressBar" style="font-size: 1px; height: 20px; width: 0px; border: 1px solid white;"> </div> </div> </td> |
into a registration form
Inspired from : https://www.microsoft.com/protect/yourself/password/checker.mspx and
http://www.codeandcoffee.com/2007/06/27/how-to-make-a-password-strength-meter-like-google/
Last edited by gidevelop on Wed Jan 09, 2008 1:27 pm; edited 1 time in total
