Last active
August 29, 2015 14:01
-
-
Save bitbonsai/304cfa43a3c7c124bb88 to your computer and use it in GitHub Desktop.
Password Strenght JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scorePassword(pass) { | |
var score = 0; | |
if (!pass) | |
return score; | |
// award every unique letter until 5 repetitions | |
var letters = new Object(); | |
for (var i=0; i<pass.length; i++) { | |
letters[pass[i]] = (letters[pass[i]] || 0) + 1; | |
score += 5.0 / letters[pass[i]]; | |
} | |
// bonus points for mixing it up | |
var variations = { | |
digits: /\d/.test(pass), | |
lower: /[a-z]/.test(pass), | |
upper: /[A-Z]/.test(pass), | |
nonWords: /\W/.test(pass), | |
} | |
variationCount = 0; | |
for (var check in variations) { | |
variationCount += (variations[check] == true) ? 1 : 0; | |
} | |
score += (variationCount - 1) * 10; | |
return parseInt(score); | |
} | |
function checkPassStrength(pass) { | |
var score = scorePassword(pass); | |
if (score > 80) | |
return "strong"; | |
if (score > 60) | |
return "good"; | |
if (score >= 30) | |
return "weak"; | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment