密码验证至少为6个字符

ina*_*naz 24 javascript css validation jquery

我为密码写了一些验证码,并确认密码是否匹配.此外,还有一个条件是检查我的密码长度是否小于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!"
    }
}  
Run Code Online (Sandbox Code Playgroud)
         <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>
        
Run Code Online (Sandbox Code Playgroud)

小智 12

试试这个:

if(pass1.value.length > 5)
Run Code Online (Sandbox Code Playgroud)


小智 12

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

祝好运!

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"
    }
}  
Run Code Online (Sandbox Code Playgroud)
<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>
        
Run Code Online (Sandbox Code Playgroud)


Vur*_*ral 8

之前:

 if(pass1.length > 5)
Run Code Online (Sandbox Code Playgroud)

后:

 if(pass1.value.length > 5)
Run Code Online (Sandbox Code Playgroud)

你应该在一切都适合之后检查平等,比如长度或允许的字符等.

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 && pass2.value.length > 5)
    {
        pass1.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "character number ok!"

        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"
        }
    }
    else
    {
        pass1.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = " you have to enter at least 6 digit!"
    }

}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


WSk*_*WSk 7

if (pass1.value.length > 5)
Run Code Online (Sandbox Code Playgroud)

确保不应用trim()因为trim将删除空格并且密码中的空格不是有效字符.


Łuk*_*wik 6

当您检查pass1的长度时,您实际上并没有检查它的值pass1.length > 5- 您应该将其更改为pass1.value.length > 5


Cod*_*ogi 6

你可以做:

if (pass1.value.trim().length > 5)
Run Code Online (Sandbox Code Playgroud)


小智 6

要检查长度,你必须这样写

if (pass1.value.length > 5)
Run Code Online (Sandbox Code Playgroud)

然后你的代码工作正常


Ama*_*wal 5

您可以利用正则表达式为您进行验证.例如:我在此密码中允许一些特殊字符,并且计数大于6

regex = pass1.value.search(/^[a-zA-Z0-9+&@#/%?=~_|!:,.;]{6,}+$/);

if(regex){
    message.innerHTML="Invalid Password.";
}else{
    message.innerHTML = " you have to enter at least 6 digit!";
}
Run Code Online (Sandbox Code Playgroud)