gpt4 book ai didi

javascript - 密码验证至少为 6 个字符

转载 作者:技术小花猫 更新时间:2023-10-29 10:09:38 26 4
gpt4 key购买 nike

我为密码编写了一些验证代码并确认密码以检查它们是否匹配。此外,还有一个条件会检查我的密码长度是否少于 6 个字符,如果少于 6 个字符则写入/显示错误。但是我的代码不能正常工作:当我切换到字段 2 时,字段 1 的条件没有被检查,如果两个条件都正确,错误仍然存​​在。

这是我的代码:

function checkPass()
{
var pass1 = document.getElementById('pass1');
var pass2 = document.getElementById('pass2');
var message = document.getElementById('error-nwl');
var goodColor = "#66cc66";
var badColor = "#ff6666";

if(pass1.value == pass2.value){
pass2.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "ok!"
}
else{
pass2.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = " These passwords don't match"
}

if(pass1.length > 5){
pass1.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "character number ok!"
}
else{
pass1.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = " you have to enter at least 6 digit!"
}
}
         <input name="password" type="password" placeholder="password"  id="pass1"/>
<input name="repeatpassword" type="password" placeholder="confirm password" id="pass2" onkeyup="checkPass(); return false;" />
<div id="error-nwl"></div>

最佳答案

使用下面的代码。 首先pass1.length不正确。您应该编写 pass1.value.length其次,我在最后添加了比较两个 block ,因为首先你应该检查第一个 block 的长度。此外,还应在第一个 block 发生更改时调用该函数。

祝你好运!

function checkPass()
{
var pass1 = document.getElementById('pass1');
var pass2 = document.getElementById('pass2');
var message = document.getElementById('error-nwl');
var goodColor = "#66cc66";
var badColor = "#ff6666";

if(pass1.value.length > 5)
{
pass1.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "character number ok!"
}
else
{
pass1.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = " you have to enter at least 6 digit!"
return;
}

if(pass1.value == pass2.value)
{
pass2.style.backgroundColor = goodColor;
message.style.color = goodColor;
message.innerHTML = "ok!"
}
else
{
pass2.style.backgroundColor = badColor;
message.style.color = badColor;
message.innerHTML = " These passwords don't match"
}
}
<input name="password" type="password" placeholder="password"  id="pass1" onkeyup="checkPass(); return false;" />
<input name="repeatpassword" type="password" placeholder="confirm password" id="pass2" onkeyup="checkPass(); return false;" />
<div id="error-nwl"></div>

关于javascript - 密码验证至少为 6 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39740832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com