简体   繁体   English

javascript错误中的密码和确认密码验证?

[英]Password and confirm password validation in javascript error?

 var password = document.getElementById("password"); var confirm_password = document.getElementById("confirm_password"); function validatePassword() { if(password.value != confirm_password.value) { confirm_password.setCustomValidity("Passwords Don't Match"); } else if(password.value=='') { password.setCustomValidity("Passwords must not be empty"); } else { password.setCustomValidity(''); } } password.onchange = validatePassword; confirm_password.onkeyup = validatePassword;
 <input type="password" name="password" id="password" placeholder="Please Enter Password" required="required"/> <input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm Password" required="required"/>

I need to show password empty message while submitting without typing anything.我需要在提交时显示密码为空消息而不输入任何内容。
It validates mismatching but it is not validating empty fields.它验证不匹配,但不验证空字段。 Any solution?有什么解决办法吗?

Try using like this, with ||尝试像这样使用|| (or) condition, (或)条件,

<input type="password" name="password" id="password" placeholder="Please Enter Password" required="required"/>
<input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm Password"  required="required"/>

<script>
    var password = document.getElementById("password");
    var confirm_password = document.getElementById("confirm_password");

function validatePassword()
{
    if(password.value != confirm_password.value)
    {
        confirm_password.setCustomValidity("Passwords Don't Match");
    } 
    else if(password.value == '' || password.value == undefined || password.value == null)
    {
        password.setCustomValidity("Passwords must not be empty");
    } 
    else
    {
        password.setCustomValidity('');
    }
}
password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
</script>

It must be in this sequence.必须是这个顺序。 Check empty value first.首先检查空值。 try this试试这个

    if(password.value=="")
    {
        password.setCustomValidity("Passwords must not be empty");
    } 
    else if(password.value != confirm_password.value)
    {
        confirm_password.setCustomValidity("Passwords Don't Match");
    } 
    else
    {
        password.setCustomValidity('');
    }

Since you're not setting the setCustomValidity() inline in your <input> tag, you can remove even your required attribute and leave all your validations to your JS script (just make sure you handle your submit event properly, eg with e.preventDefault() ) .由于您没有在<input>标签中设置内联setCustomValidity() ,您甚至可以删除required属性并将所有验证留给您的 JS 脚本(只需确保正确处理您的submit event ,例如使用e.preventDefault() ) This is to avoid the default validation from triggering.这是为了避免触发默认验证。 Secondly, in your logic, your password.setCustomValidity('');其次,在你的逻辑中,你的password.setCustomValidity(''); is not resetting at all each time either one of the above two conditions is met.每次满足上述两个条件之一时都不会重置。

So for your case, to be safe, try this:因此,对于您的情况,为了安全起见,请尝试以下操作:

Remove the required attribute and add the oninput attribute:删除required属性并添加oninput属性:

<input type="password" name="password" id="password" placeholder="Please Enter Password" oninput="this.setCustomValidity('')" />

The oninput="this.setCustomValidity('')" will ensure the custom validity is reset each time you edit the password. oninput="this.setCustomValidity('')"将确保每次编辑密码时重置自定义有效性。 Then in your JS code:然后在你的 JS 代码中:

function validatePassword()
{
    if(password.value.trim() == "")
    {
        password.setCustomValidity("Passwords must not be empty");
        password.reportValidity();
    } 
    else if(password.value != confirm_password.value)
    {
        confirm_password.setCustomValidity("Passwords Don't Match");
        confirm_password.reportValidity();
    }; /* else
    {
        password.setCustomValidity("");
    } */
}

Note that if you don't change the sequence of your logic, the "Passwords must not be empty" can never be triggered whenever the confirm_password has value -- although it can still trigger the mismatch error.请注意,如果您不更改逻辑顺序,则只要confirm_password具有值,就永远不会触发"Passwords must not be empty" ——尽管它仍然可以触发mismatch错误。 It only works when both are empty .它仅在两者都为空时有效

You have to use .reportValidity() for the warning will actually show up.您必须使用.reportValidity()才能实际显示警告。

 var password = document.getElementById("password"); var confirm_password = document.getElementById("confirm_password"); function validatePassword() { if(password.value != confirm_password.value) { confirm_password.setCustomValidity("Passwords Don't Match"); confirm_password.reportValidity(); } else if(password.value.trim() == "") { password.setCustomValidity("Passwords must not be empty"); password.reportValidity(); } else { password.setCustomValidity(""); } } password.onchange = validatePassword; confirm_password.onkeyup = validatePassword;
 <input type="password" name="password" id="password" placeholder="Please Enter Password" required /> <input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm Password" required />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM