jquery - 表单验证所需的规则取决于

时间:2012-06-05 22:07:26

标签: jquery jquery-validate

有人可以给我介绍如何使用validaterulesrequireddepends。 我有一些部分代码但不确定它是如何工作的。

$("#form2").validate({
    rules: {
        firstname: {
            required: {
                depends: function () {
                    $(this).val($.trim($(this).val()));
                    return true;
                }
            },
            minlength: 2
        },

1 个答案:

答案 0 :(得分:4)

$("#form2").validate({
rules: {
    firstname: {     //This rule applies to the $('#form2 input[name="firstname"]') selector

        required: {  //This can be a true/false boolean, string, or a complex field using depends.

            depends: function () { //depends takes a function pointer that
                                   //returns a value, informing the rule 
                                   //if it is required. In this case, always true.

                $(this).val($.trim($(this).val()));

                return true;
            }
        },

        minlength: 2 // the minimum length of text in the field is 2 characters.
    },