jquery身份验证密码不匹配

时间:2014-03-31 10:29:17

标签: jquery html forms authentication forms-authentication

如果你能告诉我这段代码出了什么问题,我真的很感激 - 我是一个初学者,所以要轻松一点!

我有一个如下表单和一些基本的jQuery身份验证。一切似乎工作得很好接受检查密码是否匹配,这告诉我密码每次都匹配,对于我的生活,我无法弄清楚是什么。

Html表格:

 <form name="signupform" id="signupform" action="signup.php" method="post"> 
<div>Email Address</div>
<input id="email" name="email" type="text" maxlength="88">
<span id="emailstatus"></span>
<div>Full Name</div>
<input id="personname" name="personname" type="text" maxlength="20">
<div>Create Password</div>
<input id="password1" name="password1" type="password" maxlength="100">
<div>Confirm Password</div>
<input id="password2" name="password2" type="password" maxlength="100"> 
<div>Address Line 1</div>
<input id="address_line1" name="address_line1" type="text" maxlength="100">
<div>Address Line 2</div>
<input id="address_line2" name="address_line2" type="text" maxlength="100">
<div>City</div>
<input id="address_city" name="address_city" type="text" maxlength="100">
<div>State</div>
<input id="address_state" name="address_state" type="text" maxlength="100">
<div>Post Code/ ZIP Code</div>
<input id="address_zip" name="address_zip" type="text" maxlength="100">
<div>Country</div>
<select id="address_country" name="address_country">
  <?php include_once("template_country_list.php"); ?>
</select><br><br>

<button class="inline-block" id="signupbtn">Create Account</button>
<p id="error">There were errors on the form, please make sure all fields are fill out correctly.</p>
<p id="passmatcherror">Passwords do not match.</p>

Jquery的:

$(document).ready(function(){
var pname = $("#personname");
var e = $("#email");
var p1 = $("#password1");
var p2 = $("#password2");
var country = $("#address_country");
var add1 = $("#address_line1");
var add2 = $("#address_line2");
var city = $("#address_city");
var state = $("#address_state");
var zip = $("#address_zip");
var status = $("#status");
// Place ID's of all required fields here.
required = ["personname", "email", "password1", "password2", "address_country", "address_line1", "address_line2", "address_city", "address_state", "address_zip"];
// If using an ID other than #email or #error then replace it here
email = $("#email");
errornotice = $("#error");
passmatcherror = $("#passmatcherror");
passmatcherror.hide();
// The text to show up within a field when it is incorrect
emptyerror = "";
emailerror = " Please enter a valid e-mail.";
$("#signupform").submit(function(){ 
    //Validate required fields
    for (i=0;i<required.length;i++) {
        var input = $('#'+required[i]);
        if ((input.val() == "") || (input.val() == emptyerror)) {
            input.addClass("needsfilled");
            input.val(emptyerror);
            errornotice.fadeIn(750);
        }

         else {
            input.removeClass("needsfilled");
        }
    }
    // Validate the e-mail.
    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
        email.addClass("needsfilled");
        email.val(emailerror);
    }

    if(p1 != p2){
            passmatcherror.fadeIn(750);
            p1.addClass("needsfilled");
            p2.addClass("needsfilled");

        }


    //if any inputs on the page have the class 'needsfilled' the form will not submit
    if ($(":input").hasClass("needsfilled")) {
        return false;
    } else {
        errornotice.hide();
        return true;
    }
});

// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){       
   if ($(this).hasClass("needsfilled") ) {
        $(this).val("");
        $(this).removeClass("needsfilled");
        errornotice.hide();
        passmatcherror.hide();
       }
        });
}); 

1 个答案:

答案 0 :(得分:0)

在本节中,您似乎在比较两个jquery对象而不是它们的值:

if(p1 != p2){
    passmatcherror.fadeIn(750);
    p1.addClass("needsfilled");
    p2.addClass("needsfilled");
}

如果将其更改为

会发生什么
if(p1.val() !== p2.val()){
    passmatcherror.fadeIn(750);
    p1.addClass("needsfilled");
    p2.addClass("needsfilled");
}