Skip to content

Instantly share code, notes, and snippets.

@soldair
Created May 25, 2010 07:39
Show Gist options
  • Save soldair/412891 to your computer and use it in GitHub Desktop.
Save soldair/412891 to your computer and use it in GitHub Desktop.
/*javascript rank password function
copyright ryanday 2010
mit/gpl license
feel free to use and include as long as the code retains the above copyright. =)
PURPOSE:
i needed to present users with a password quality visual alert on keyup for the password on a registration form. this is the core function with no depends
USE://jquery example
$(".password-input").bind('keypress',function(){
var score = rankPassword($(this).val());
if(score >= 4) {
alert('strong');
} else if(score == 3) {
alert('good');
} else if(score == 2){
alert('okay');
} else {
alert('weak');
}
});
*/
var rankPassword = function(pw){
pw = $.trim(pw);
var checks = {
length:function(pw){
return pw.length >= 6;
},
digit:function(pw){
var l = pw.length;
var all_alpha = new RegExp("/[a-z]{"+l+","+l+"}/i");
var all_digit = new RegExp("/[0-9]{"+l+","+l+"}/i");
return !pw.match(all_alpha) && !pw.match(all_digit);
},
punct:function(pw){
return pw.match(/[^[a-z0-9]+/i);
},
upper:function(pw){
return pw.toLowerCase() != pw && pw.toUpperCase() != pw
},
reallylong:function(pw){
return pw.length >= 9;
}
};
var score = 0;
if(checks.length(pw)){
for( var k in checks){
score += (checks[k](pw)?1:0);
}
}
return score;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment