0

我的设置是我有一个名为 claimaint_rep 的字段,现在当该值为 Yes 时,我希望需要多个字段。现在我正在使用 jquery 验证。我目前的代码是:

 $('#aspnetForm').validate({
        errorPlacement: function (error, element) { },

        rules:
                {
                    claimsol:
                        {
                            required: function (element) {
                                return $("#ctl00_ContentPlaceHolder1_claimant_rep").val() == "Yes";
                            },
                            minlength: 2
                        }

                },

        highlight: function (element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');
        },
        success: function (element) {
            $(element).closest('.control-group').removeClass('error').addClass('success');
        }
    });

示例 HTML:

<div class="control-group">
    <label class="control-label" for="inputEmail">Reference Number</label>
    <div class="controls">      
        <asp:TextBox ID="solicitor_reference" CssClass="span3 claimsol" runat="server"></asp:TextBox>
    </div>
  </div>

字段命名是因为它是一个 ASP Webform(呃),现在看来,claimsol 规则将只适用于一个名为 claimol 的字段?我想将该规则应用于多个字段,理想情况下不必在规则部分中设置每个字段。我在这里想念什么?

4

1 回答 1

2

Quote OP:

I want to apply that rule to multiple fields, ideally without having to set each one in the rules section.

I really have no idea what you're ultimately trying to achieve, but there are only a couple of methods that can apply rules in bulk.

1) rules('add') method for dynamically adding rules to a jQuery target selector. If the selector targets multiple fields, you must wrap this method inside an .each().

$('[name*="field"]').each(function() {
    $(this).rules('add', {
        required: true,
        number: true,
        messages: { // optional custom messages
            required:  "your custom message",
            number:  "your custom message"
        }
    });
});

The various ways to implement this are limitless.

See: https://stackoverflow.com/a/15116062/594235

2) addClassRules() method for creating a class name that represents your compound rule. (However, over-riding the default error messages is not as simple as in item #1).

$.validator.addClassRules("myclass", {
    required: true,
    minlength: 2
});

You must also add that class name to each input that needs to use that rule.

<input type="text" class="myclass" name="field" />

DEMO: http://jsfiddle.net/62uua/

3) Or you can create a whole new rule based on your dependency function using the addMethod() method.

$.validator.addMethod("myrule", function(value, element) { 
    // your function
    // return false triggers the error
    // return true passes validation
}, "Custom message");

See the documentation for all available methods and usage: http://docs.jquery.com/Plugins/Validation


EDIT:

If you're trying to suppress error messages, do not pass an empty callback function like you did here,

errorPlacement: function (error, element) { },

pass a return false instead,

errorPlacement: function (error, element) { 
    return false;  // suppress default error message placement
},
于 2013-04-16T14:14:47.533 回答