I'm trying to create a simple password strength indicator with javascript by changing the value of the html5 progress element, but somethings wrong, can somebody point out the error here please? Console is showing zero errors and it must be something to do with change function not binding to the input with ID 'pwd'?
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Password Meter</title>
<script>
window.addEventListener('load', function() {
var password = document.getElementById("pwd");
password.addEventListener('change', function() {
// Reset if password length is zero
if (password.length === 0) {
document.getElementById("progresslabel").innerHTML = "";
document.getElementById("progress").value = "0";
return;
}
// Password requirements
var match = new Array();
match.push("[$@$!%*#?&]"); // Special Chars
match.push("[A-Z]"); // Uppercase
match.push("[0-9]"); // Numbers
match.push("[a-z]"); // Lowercase
// Check progress
var prog = 0;
for (var i = 0; i < match.length; i++) {
if (new RegExp(match[i]).test(password)) {
prog++;
}
}
//Length must be at least 8 chars
if(prog > 2 && password.length > 7){
prog++;
}
// Display it
var progress = "";
var strength = "";
switch (prog) {
case 0:
case 1:
case 2:
strength = "25%";
progress = "25";
break;
case 3:
strength = "50%";
progress = "50";
break;
case 4:
strength = "75%";
progress = "75";
break;
case 5:
strength = "100% - Password strength is good";
progress = "100";
break;
}
document.getElementById("progresslabel").innerHTML = strength;
document.getElementById("progress").value = progress;
});
});
</script>
</head>
<body>
<form>
<div>
<label for="pwd">Password:</label>
<input type="text" id="pwd">
<progress id="progress" value="0" max="100">70</progress>
<span id="progresslabel"></span></div>
</form>
</body>
</html>