我正在制作一份注册表格,我想检查用户在打字时是否有匹配的密码并给他相应的信息。到目前为止,一切都有效,除了这些事情:
这是我的代码:
$(function () {
$("#txtNewPassword").keyup(checkPasswordMatch);
$("#txtConfirmPassword").keyup(checkPasswordMatch);
});
function checkPasswordMatch() {
$("#divCheckPasswordMatch").html("");
var password = $("#txtNewPassword").val();
var confirmPassword = $("#txtConfirmPassword").val();
if (password == "" && confirmPassword == ""){
$("#divCheckPasswordMatch").html("");
$("#divIsPasswordExist").html("");
}
else if (password != "" && confirmPassword == "") {
$("#divCheckPasswordMatch").html("");
}
else if (password == "" && confirmPassword != "")
$("#divIsPasswordExist").html("Password cannot be empty!");
else
$("#divIsPasswordExist").html("");
if (password != confirmPassword)
{
$("#divCheckPasswordMatch").removeClass("registrationFormConfirm");
$("#divCheckPasswordMatch").addClass("registrationFormAlert");
$("#divCheckPasswordMatch").html("Passwords do not match!");
}
else
{
$("#divCheckPasswordMatch").removeClass("registrationFormAlert");
$("#divCheckPasswordMatch").addClass("registrationFormConfirm");
$("#divCheckPasswordMatch").html("Passwords match.");
}
}
请问任何想法? 谢谢!
答案 0 :(得分:2)
那是因为if (password != confirmPassword)
是假的,因为两者都是空的,当你设置var password = $("#txtNewPassword").val();
和var confirmPassword = $("#txtConfirmPassword").val();
这两者都相等时。在检查值是否为空之后应该添加一个返回值,这样当用户删除文本时,它不会检查第一个检查语句下面的任何内容。
//首先检查
if (password == "" && confirmPassword == ""){
return false;
}
......代码的其余部分
答案 1 :(得分:1)
if (password == "" && confirmPassword == ""){
return false;
}
....
....
if(confirmPassword != "")
$("#txtConfirmPassword").html("Please confirm password");
if (password != confirmPassword)
{
$("#divCheckPasswordMatch").removeClass("registrationFormConfirm");
$("#divCheckPasswordMatch").addClass("registrationFormAlert");
$("#divCheckPasswordMatch").html("Passwords do not match!");
}
else
{
$("#divCheckPasswordMatch").removeClass("registrationFormAlert");
$("#divCheckPasswordMatch").addClass("registrationFormConfirm");
$("#divCheckPasswordMatch").html("Passwords match.");
}
...
...
答案 2 :(得分:1)
......
if (password == "" && confirmPassword == ""){
$("#divCheckPasswordMatch").html("");
$("#divIsPasswordExist").html("Password and Confirm Password dosen't exists");
return;
}
else if (password != "" && confirmPassword == "") {
$("#divCheckPasswordMatch").html("Confirm Password dosent't exists");
return;
}
else if (password == "" && confirmPassword != ""){
$("#divIsPasswordExist").html("Password cannot be empty!");
return;
}
else{
$("#divIsPasswordExist").html("");
}
if (password != confirmPassword)
{
$("#divCheckPasswordMatch").removeClass("registrationFormConfirm");
$("#divCheckPasswordMatch").addClass("registrationFormAlert");
$("#divCheckPasswordMatch").html("Passwords do not match!");
}
else
{
$("#divCheckPasswordMatch").removeClass("registrationFormAlert");
$("#divCheckPasswordMatch").addClass("registrationFormConfirm");
$("#divCheckPasswordMatch").html("Passwords match.");
}
.........
答案 3 :(得分:1)
有一种更优雅的方式来做到这一点。将您的条件更改为此
if(password.length > 0 && confirmPassword.length >0) {
if(password == confirmPassword) {
//confirmed
} else {
// not confirm
}
} else {
//not entered
}