Setting same properties for more fields

后端 未结 5 836
生来不讨喜
生来不讨喜 2021-01-26 19:40

I have two or more text fields and I want to apply the same properties to them, avoiding to write two or more times the same code

This doesn\'t work:

$(\         


        
相关标签:
5条回答
  • 2021-01-26 19:57

    Taking from my previous answer:


    For the purposes of my example, this is the base starting code:

    HTML:

    <input type="text" name="field_1" />
    <input type="text" name="field_2" />
    <input type="text" name="field_3" />
    

    jQuery:

    $('#myForm').validate({
        rules: {
            field_1: {
                required: true,
                number: true
            },
            field_2: {
                required: true,
                number: true
            },
            field_3: {
                required: true,
                number: true
            }
        }
    });
    

    DEMO: http://jsfiddle.net/rq5ra/


    Option 1a) You can assign classes to your fields based on desired common rules and then assign those rules to the classes. You can also assign custom messages.

    HTML:

    <input type="text" name="field_1" class="num" />
    <input type="text" name="field_2" class="num" />
    <input type="text" name="field_3" class="num" />
    

    The .rules('add') method must be called after invoking .validate() and .each() must be used to target all elements with the class.

    jQuery:

    $('#myForm').validate({
        // your other plugin options
    });
    
    $('.num').each(function() {
        $(this).rules('add', {
            required: true,
            number: true,
            messages: {
                required:  "your custom message",
                number:  "your custom message"
            }
        });
    });
    

    DEMO: http://jsfiddle.net/rq5ra/1/

    Option 1b) Same as above, but instead of using a class, it matches a common part of the name attribute:

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

    DEMO: http://jsfiddle.net/rq5ra/6/


    Option 2a) You can pull out the groups of rules and combine them into common variables.

    var ruleSet1 = {
            required: true,
            number: true
        };
    
    $('#myForm').validate({
        rules: {
            field_1: ruleSet1,
            field_2: ruleSet1,
            field_3: ruleSet1
        }
    });
    

    DEMO: http://jsfiddle.net/rq5ra/4/


    Option 2b) Related to 2a above but depending on your level of complexity, can separate out the rules that are common to certain groups and use .extend() to recombine them in an infinite numbers of ways.

    var ruleSet_default = {
            required: true,
            number: true
        };
    
    var ruleSet1 = {
            max: 99
        };
    $.extend(ruleSet1, ruleSet_default); // combines defaults into set 1
    
    var ruleSet2 = {
            min: 3
        };
    $.extend(ruleSet2, ruleSet_default); // combines defaults into set 2
    
    var ruleSet3 = { };
    $.extend(ruleSet3, ruleSet1, ruleSet2); // combines sets 2 & 1 into set 3.  Defaults are included since they were already combined into sets 1 & 2 previously.
    
    $('#myForm').validate({
        rules: {
            field_1: ruleSet2,
            field_2: ruleSet_default,
            field_3: ruleSet1,
            field_4: ruleSet3
        }
    });
    

    End Result:

    • field_1 will be a required number no less than 3.
    • field_2 will just be a required number.
    • field_3 will be a required number no greater than 99.
    • field_4 will be a required number between 3 and 99.

    DEMO: http://jsfiddle.net/rq5ra/5/

    0 讨论(0)
  • 2021-01-26 19:59

    apply a css class to all fields

    $(".cssClassName").rules("add", {
            required: true,  
             minlength: 3,
              maxlength: 50 ,
            messages:
       {required:"error1",minlength:"error2",maxlength:"error3"}});
    
    0 讨论(0)
  • 2021-01-26 20:17

    Clearly there are a few ways to do this but if you use a class rule:

    $.validator.addClassRules("name", {
      required: true,  
      minlength: 3,
      maxlength: 50
    });
    

    then you can set your rules like so

    $("#form").validate({  
        rules:  
        {
         firstname : name, 
         surname : name
        }
    });
    

    or set the rule using class name:

    <input id="firstname" name="firstname" class="name">
    <input id="surname" name="surnname" class="name">
    

    This method is discussed in the refactoring rules section of the jQuery validate general guidelines.

    0 讨论(0)
  • 2021-01-26 20:18

    Try Like this

    var ruleSet1 = {
            required: true,  
            minlength: 3,
            maxlength: 50
        };
    
    $('#form').validate({
        rules: {
            field_1: ruleSet1,
            field_2: ruleSet1,
            field_3: ruleSet1
        }
    });
    

    or do it one by one like the following,

    $("#form").validate({  
        rules:  
        {
        name: { required: true },
        surname:{  
            required: true,  
            minlength: 3,
            maxlength: 50   
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-26 20:24

    Define your properties as a variable, and then use it:

    var name_reqs = {  
         required: true,  
        minlength: 3,
        maxlength: 50   
    };
    
    $("#form").validate({  
        rules: {
               'name': name_reqs,
            'surname': name_reqs
        }
    });   
    
    0 讨论(0)
提交回复
热议问题