【问题标题】:jquery authentication passwords not matchingjquery身份验证密码不匹配
【发布时间】:2014-03-31 10:29:17
【问题描述】:

如果您能告诉我这段代码哪里出了问题,我将不胜感激 - 我是初学者,所以请大家放轻松!

我有一个如下表格和一些基本的 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();
       }
        });
}); 

【问题讨论】:

    标签: jquery html forms authentication forms-authentication


    【解决方案1】:

    在本节中,您似乎在比较两个 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");
    }
    

    【讨论】:

    • 太棒了!现在可以了!非常感谢 - 我不会再犯这个错误了:D
    • 我会投票给你 - 但我才刚刚加入,所以我不能:(抱歉
    • 没问题,我们都从某个地方开始!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多