我正在尝试通过更改 html5 进度元素的值来使用 javascript 创建一个简单的密码强度指示器,但是出了点问题,有人可以在这里指出错误吗?控制台显示零错误,这一定与更改功能未绑定到 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>