我正在做密码匹配的概念,当我在其中输入密码时,它工作得很好,它显示“密码匹配”或“密码不匹配”。但是这里有一个问题,或者可以说是bug之类的东西。当您的密码匹配,然后突然删除框中的两个密码,它仍然显示“密码匹配”或“密码不匹配”。为了更好的理解,我发布了一张图片。
那么,当两个密码都为空时,如何隐藏或删除“密码不匹配或密码匹配”注释。我将下面的代码发布在我所做的事情上
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
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);
});
发布于 2018-05-28 18:16:42
您应该添加一个测试来检查密码字段是否为空。
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);
});
或者使用$("#divCheckPassword").html("");
而不是$("#divCheckPassword").html("Please fill both password fields");
来清除消息。
发布于 2018-05-28 18:16:23
添加检查以检查密码文本框是否为空或已填充。如果文本框为空,请清除divCheckPassword
发布于 2018-05-28 18:53:32
只需先检查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>
https://stackoverflow.com/questions/50564006
复制相似问题