jQuery验证插件的密码确认问题

时间:2012-06-17 08:58:42

标签: jquery jquery-validate

我无法理解为什么我的代码不起作用,密码确认不会问 - “密码需要确认”。

我的js代码:

    $(document).ready(function(){
$('.new_user').validate({
    debug: true,
    rules: {
        'user[email]':{
            required: true
        },
        'user[password]': {
            required: true,
            'user[password_confirmation]': {
                equalTo: "#user_password"
            }
        }

},

showErrors: function(errorMap, errorList) {
    this.defaultShowErrors();
},
success: "valid"

});
})   

我的HTML代码:

   <div><label for="user_password">Password</label><br>
   <input id="user_password" name="user[password]" size="30" type="password" class="error"><label for="user_password" generated="true" class="error" style="">This field is required.</label></div>

      <div><label for="user_password_confirmation">Password confirmation</label>
<input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password"></div>

1 个答案:

答案 0 :(得分:3)

工作演示 http://jsfiddle.net/g45fM/1/

这个版本的演示版比你正在尝试的更干净,而且你的HTML中也缺少form

希望这有帮助。

<强>码

$.validator.setDefaults({
    submitHandler: function() {
        alert("submitted!");
    }
});

// validate signup form on keyup and submit
$("form").validate({
    rules: {
        pass: {
            required: true,
            minlength: 5
        },
        pass2: {
            required: true,
            minlength: 5,
            equalTo: "#pass"
        }
    },
    messages: {
        pass: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long"
        },
        pass2: {
            required: "Please provide a password",
            minlength: "Your password must be at least 5 characters long",
            equalTo: "Please enter the same password as above"
        }
    }
});​