使用jQuery Validate验证两个字段

时间:2011-05-17 08:28:00

标签: jquery validation

我正在使用jQuery Validate插件: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

简单演示:

  <input type="text" name="email" class="required email" />
  <input type="text" name="email_confirm" class="required email" />

如何检查两个输入中的值是否与jQuery或jQuery Validate Plugin相同。

我刚试过equalto并且它有效:

rules: {
  email: 'required',
  email2: {equalTo: '.email'}

1 个答案:

答案 0 :(得分:13)

通过查看http://jquery.bassistance.de/validate/demo/处的演示页面,您可以使用“equalTo”验证两个应该相等的字段:

    $("#signupForm").validate({
    rules: {
                    ...snip... 
        password: {
            required: true,
            minlength: 5
        },
        confirm_password: {
            required: true,
            minlength: 5,
            equalTo: "#password"
        },
                   ...snip...
    },
    messages: {
        ...snip...
        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"
        },
        ...snip...
    }
});

要使用类方法,只需要找到语法。我找不到equalTo属性,但它可能是这样的:

class="required email equalTo['email']"