Setting same properties for more fields

后端 未结 5 843
生来不讨喜
生来不讨喜 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: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:

    
    
    

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

提交回复
热议问题