简体   繁体   English

密码为空时如何禁用消息

[英]How to disable a message when the password value is empty

I was doing password matching concept and it was working fine when i entered the passwords in it and it shows "Passwords match" or "Passwords didn't match". 我正在执行密码匹配的概念,当我在其中输入密码时,它工作正常,它显示“密码匹配”或“密码不匹配”。 But here is the problem or can say bug kind of thing. 但是这里有问题或可以说是臭虫之类的东西。 When your passwords match and then suddenly you remove both the passwords in the boxes it still shows "passwords match" or "passwords didn't match". 当您的密码匹配时,突然您在框中删除了两个密码,它仍然显示“密码匹配”或“密码不匹配”。 I am posting an image of for better understanding. 我张贴的图片是为了更好的理解。

在此处输入图片说明

So how to hide or remove that "passwords didn't match or passwords match" comment when both the passwords are empty in the boxes. 因此,当两个密码在框中均为空时,如何隐藏或删除“密码不匹配或密码匹配”注释。 I am posting the code below on what i did 我将以下代码发布到我所做的事情中

HTML HTML

<p>
    <input type="password" name="NPassword" placeholder="New Password" id="txtNewPassword" />
</p>
<input type="password" name="RNPassword" placeholder="Retype New Password" id="txtConfirmPassword" onChange="isPasswordMatch();" />
<div id="divCheckPassword"></div>

JavaScript JavaScript的

function isPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword) $("#divCheckPassword").html("Passwords do not match!");
    else $("#divCheckPassword").html("Passwords match.");


}

$(document).ready(function () {
    $("#txtConfirmPassword").keyup(isPasswordMatch);
});

Add a check for checking if the passwords text box are empty or filled. 添加检查以检查密码文本框是否为空或已填充。 If text boxes are empty clear divCheckPassword 如果文本框为空,请清除divCheckPassword

You should add a test to check if the passwords fields are empty. 您应该添加测试以检查密码字段是否为空。

function isPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword) $("#divCheckPassword").html("Passwords do not match!");
    else if ((password ==  "") || (confirmPassword == "")) $("#divCheckPassword").html("Please fill both password fields"); 
    else $("#divCheckPassword").html("Passwords match.");


}

$(document).ready(function () {
    $("#txtConfirmPassword").keyup(isPasswordMatch);
});

Or use $("#divCheckPassword").html(""); 或使用$("#divCheckPassword").html(""); instead of $("#divCheckPassword").html("Please fill both password fields"); 而不是$("#divCheckPassword").html("Please fill both password fields"); to clear the message. 清除消息。

simply first check if the input is empty or not. 只需首先检查input是否为空。

 function isPasswordMatch() { var password = $("#txtNewPassword").val(); var confirmPassword = $("#txtConfirmPassword").val(); if(password =='' || confirmPassword=='') $("#divCheckPassword").html(''); else if(password != confirmPassword) $("#divCheckPassword").html("Passwords do not match!"); else $("#divCheckPassword").html("Passwords match."); } $(document).ready(function () { $("#txtConfirmPassword").keyup(isPasswordMatch); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <input type="password" name="NPassword" placeholder="New Password" id="txtNewPassword" /> </p> <input type="password" name="RNPassword" placeholder="Retype New Password" id="txtConfirmPassword" onChange="isPasswordMatch();" /> <div id="divCheckPassword"></div> 

This code is for hiding the message when you remove password first and confirm password next or vice-versa. 此代码用于在您首先删除密码,然后再次确认密码或反之亦然时隐藏消息。

 function isPasswordMatch() { var password = $("#txtNewPassword").val(); var confirmPassword = $("#txtConfirmPassword").val(); if (password != confirmPassword) $("#divCheckPassword").html("Passwords do not match!"); else $("#divCheckPassword").html("Passwords match."); if ((password == "") && (confirmPassword == "")) $("#divCheckPassword").hide(); if ((confirmPassword == "") && (password == "")) $("#divCheckPassword").hide(); } $(document).ready(function () { $("#txtNewPassword").keyup(isPasswordMatch); $("#txtConfirmPassword").keyup(isPasswordMatch); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <input type="password" name="NPassword" placeholder="New Password" id="txtNewPassword" /> </p> <input type="password" name="RNPassword" placeholder="Retype New Password" id="txtConfirmPassword" onChange="isPasswordMatch();" /> <div id="divCheckPassword"></div> 

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

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