Wednesday, October 5, 2011

Free Simple Javascript Password Strength Checker

These codes came from many sources until I forgot the origins. I just combine all the good stuffs I found from the net. Sorry if I use your codes somewhere and remind me to include your credit as I forgot the origin of it.

The Javascript function:


function checkpassword(pw) {

var Score = 0;
var Result = "weak";

if (pw.length>5 && pw.length<8) { Score = (Score+6); }
else if (pw.length>7 && pw.length<16) { Score = (Score+12); }
else if (pw.length>15) { Score = (Score+18); }

if (pw.match(/[a-z]/)) { Score = (Score+1); }
if (pw.match(/[A-Z]/)) { Score = (Score+5); }
if (pw.match(/\d+/)) { Score = (Score+5); }
if (pw.match(/(.*[0-9].*[0-9].*[0-9])/)) { Score = (Score+5); }
if (pw.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) { Score = (Score+5); }
if (pw.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) { Score = (Score+5); }
if (pw.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) { Score = (Score+5); }
if (pw.match(/([a-zA-Z])/) && pw.match(/([0-9])/)) { Score = (Score+5); }
if (pw.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) { Score = (Score+5); }


if (pw.length == 0) { Result = ""; }
else if (pw.length < 6) { Result = "too short"; }
else if (Score < 16) { Result = "very weak"; }
else if (Score > 15 && Score < 25) { Result = " weak"; }
else if (Score > 24 && Score < 35) { Result = "mediocre"; }
else if (Score > 34 && Score < 45) { Result = "strong"; }
else { Result = "strong enough"; }
document.getElementById("yourResultDivID").innerHTML = Result;
}




The HTML:

<input type=password name=pw size=35 maxlength=12 onkeyup="javascript:checkpassword(this.value);">

<div id="yourResultDivID" style="display:inline;" width=100></div>




The thing to pay attention to is highlighted in red. Something I found many other ways in the net but this is what I like.

No comments:

Post a Comment