Skip to content

Instantly share code, notes, and snippets.

@brianteachman
Created December 4, 2012 00:40
Show Gist options
  • Save brianteachman/0dd37b51dc9caa2a01c3 to your computer and use it in GitHub Desktop.
Save brianteachman/0dd37b51dc9caa2a01c3 to your computer and use it in GitHub Desktop.
Password Scorer
// http://net.tutsplus.com/tutorials/javascript-ajax/meet-crockford%E2%80%99s-jscheck/
(function () {
var PasswordScorer = {};
PasswordScorer.score = function (password) {
var len = password.length,
lengthScore = 0,
letterScore = 0,
chars = {}
if (len >= 21) { lengthScore = 7; }
else if (len >= 16) { lengthScore = 6; }
else if (len >= 13) { lengthScore = 5; }
else if (len >= 10) { lengthScore = 4; }
else if (len >= 8) { lengthScore = 3; }
else if (len >= 5) { lengthScore = 2; }
var re = [ null, /[a-z]/g, /[A-Z]/g, /\d/g, /[!@#$%\^&\*\(\)=_+-]/g];
for (var i = 1; i < re.length; i++) {
letterScore += (password.match(re[i]) || []).length * i;
}
return letterScore + lengthScore;
};
(typeof window !== 'undefined' ? window : exports).PasswordScorer = PasswordScorer;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment