2012-06-05 16 views
2

有人可以給我一個關於如何使用validate,rules,required,depends的入門書。 我有一些部分代碼,但不知道它是如何工作的。jquery - 表單驗證規則要求取決於

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

回答

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. 
    },