equalTo()方法在jquery中不起作用?

时间:2012-07-06 11:32:30

标签: jquery validation

$('#reg_form').validate({
                onkeyup: false,
                errorClass: 'error',
                validClass: 'valid',
                rules: {
                    username: {
                        required: true,
                        minlength: 5,
                        //remote: "user_check.php"
                    },
                    password: {
                        required: true,
                        minlength: 5
                    },
                    confirm_password: {
                        required: true,
                        minlength: 5,
                        equalTo: "#password"
                    },
                    secretQuestion: "required",
                    secretAnswer: "required",
                    emailId: {
                        required: true, 
                        email: true
                    },
                    termsConditions: "required"                 
                },
                messages:{
                    username: {
                        required: "Please enter Username",
                        minlength: "Please enter atleast 5 characters",
                        //remote: jQuery.format("{0} is already in use")
                    },
                    password: {
                        required: "Please provide a password",
                        minlength: "Your password must be at least 5 characters long"
                    },
                    confirm_password: {
                        required: "Please provide a password",
                        minlength: "Your password must be at least 5 characters long",
                        equalTo: "Please enter the same password as above"
                    },
                    secretQuestion: "Please select your question",
                    secretAnswer: "Please enter your secret answer",
                    emailId: "Please enter a valid email address",
                    termsConditions: "Please accept our Terms and COnditions"
                },
                highlight: function(element) {
                    $(element).closest('div').addClass("f_error");
                },
                unhighlight: function(element) {
                    $(element).closest('div').removeClass("f_error");
                },
                errorPlacement: function(error, element) {
                    $(element).closest('div').append(error);
                }
            });

4 个答案:

答案 0 :(得分:4)

当您通过“rules:”设置验证时,您需要输入的“名称”而不是“id”。

所以对于这样的输入:

<input type="text" name="password_name" id="password_id></input>
<input type="text" name="confirm_password_name" id="confirm_password_id></input>

您应该在验证中更改此内容:

password_name: {
  required: true,
  minlength: 5
},
confirm_password_name: {
  required: true,
  minlength: 5,
  equalTo: "#password_id"
}

希望它能奏效。

答案 1 :(得分:3)

在您的代码中,似乎没有任何理由可以解决这一问题。假设您的密码输入为id="password",一切都应该没问题。

我创建了一个缩短的示例,演示equalTo规则有效:http://jsfiddle.net/ryleyb/MnnE5/

将来,对于这些问题,请尽量使用最小的示例来演示您的问题 - 我们不需要为整个表单提供规则和消息,只需要2个密码字段。发布相关的HTML表单标记也很有帮助。

答案 2 :(得分:0)

是的,它直接起作用了。

Firefox 17.0.1
jQuery 1.8.3
jQuery Validation Plugin 1.10.0

如果您只使用字段名称,则无效:

... equalTo = “密码” ....

相反,需要使用id:

... equalTo = “#密码” ....

答案 3 :(得分:0)

请确保在html页面上没有相同的ID检查包含的模板

  

注意:对于测试,只需更改id,如register_pass和login_pass,或只是pass1
        1)。如果成功,那么你的html上不是唯一的         2)。如果失败,则打开控制台以检查jquery或验证js是否可用