JavaScript 设置密码的强度校验 [ 新手入门 ]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Password Strength Checker</title>
<style>
.weak {
background-color: red;
}
.medium {
background-color: yellow;
}
.strong {
background-color: green;
}
</style>
</head>
<body>
<label for="password">Password:</label>
<input type="password" id="password" onkeyup="updatePasswordStrength()" />
<div id="password-strength"></div>
<script>
function updatePasswordStrength() {
var password = document.getElementById("password").value;
var passwordStrength = checkPasswordStrength(password);
var passwordStrengthElement = document.getElementById("password-strength");
passwordStrengthElement.className = passwordStrength;
passwordStrengthElement.textContent = passwordStrength.toUpperCase();
}
function checkPasswordStrength(password) {
var strongRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*])/;
var mediumRegex = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*])/;
if (strongRegex.test(password)) {
return "strong";
} else if (mediumRegex.test(password)) {
return "medium";
} else {
return "weak";
}
}
</script>
</body>
</html>
共 0 条回复
没有找到数据。
PHP学院的中学生
注册时间:2018-10-23
最后登录:2024-09-23
在线时长:168小时13分
最后登录:2024-09-23
在线时长:168小时13分
- 粉丝29
- 金钱4725
- 威望30
- 积分6705