用js实现一个可以用文本框输入密码的时候强度从弱中强来实现用数字大写字母和小写字母
组成的密码。
示例代码:
HTML部分:
<label>请输入密码:</label>
<input type="password" id="password">
<div id="strength"></div>
JS部分:
const passwordInput = document.getElementById("password");
const strengthDiv = document.getElementById("strength");
passwordInput.addEventListener("input", checkPasswordStrength);
function checkPasswordStrength() {
const password = passwordInput.value;
let strength = 0;
// 判断密码中是否包含数字
if (/\d/.test(password)) {
strength++;
}
// 判断密码中是否包含大写字母
if (/[A-Z]/.test(password)) {
strength++;
}
// 判断密码中是否包含小写字母
if (/[a-z]/.test(password)) {
strength++;
}
// 根据密码强度设置不同的提示信息
switch (strength) {
case 0:
strengthDiv.innerText = "";
break;
case 1:
strengthDiv.innerText = "密码强度:弱";
break;
case 2:
strengthDiv.innerText = "密码强度:中";
break;
case 3:
strengthDiv.innerText = "密码强度:强";
break;
default:
strengthDiv.innerText = "";
break;
}
}
``
原文地址: https://www.cveoy.top/t/topic/edQ7 著作权归作者所有。请勿转载和采集!