Javascript 验证密码并根据密码长度更改其颜色:
我想写一个代码,借助它我可以找到代码的长度,并根据它我可以改变错误的颜色。例如,如果密码小于4,则它将显示为红色,如果密码小于8,则显示为黄色,如果密码小于12,则显示为绿色 我的初始代码是Javascript 验证密码并根据密码长度更改其颜色:,javascript,Javascript,我想写一个代码,借助它我可以找到代码的长度,并根据它我可以改变错误的颜色。例如,如果密码小于4,则它将显示为红色,如果密码小于8,则显示为黄色,如果密码小于12,则显示为绿色 我的初始代码是 <!DOCTYPE html> <html> <head> <script type="text/javascript"> function checkPasswordLength(password){ if (password.length >
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function checkPasswordLength(password){
if (password.length > 12) {
document.getElementById("errorSpan").innerHTML = "Limit is 12characters";
}
else {
document.getElementById("errorSpan").innerHTML = "";
}
}
</script>
</head>
<body>
<form>
<input type="password" name="pwd"onchange='checkPasswordLength(this.value)'>
<span id="errorSpan" style="color:red;"></span>
</div>
</form>
</body>
</html>
你在找这样的东西吗 函数checkPasswordLengthpassword{ var span=document.getElementByIderrorSpan; 如果password.length>=12{ span.style.color=绿色; 回来 }如果password.length>=4,则为else{ span.style.color=黄色; 回来 }否则{ span.style.color=红色; } }
****嗯。那么你的问题是什么?什么有效或无效?您会遇到什么错误?应该有大量的代码片段用于获取字符串的长度。至于更改颜色,只需document.getElementByIderrorSpan.style.color=FDFF00;对于黄色和document.getElementByIderrorSpan.style.color=FF0000;红色的