Setting same properties for more fields

后端 未结 5 847
生来不讨喜
生来不讨喜 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 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   
            }
        }
    });
    

提交回复
热议问题