2013-10-03 168 views
1

經過很多努力,我完全無法讓addMethod正常工作。jquery驗證器addMethod不起作用

代碼我有如下:

$("#submitform").validate(
     { 
      rules: { 
       org_name: "required", 
       email_address: "required", 
       phone: "required", 
       abn: "required", 
       description: "required", 
       geocomplete: "required" 
      }, 
      messages: { 
       org_name: "Required", 
       email_address: "Required", 
       phone: "Required", 
       abn: "Required", 
       description: "Required", 
       geocomplete: "Required" 
      },    
      submitHandler: function(event, validator) {  


       // submitForm(); 

      }, 
      invalidHandler: function(event, validator) { 
       //code.. 
      } 
    }); 

的addMethod

$.validator.addMethod("org_name", function(value) { 
    // return this.optional(element) || value == params[0] + params[1]; 
    console.log("hi" + value);alert('hi'); 
    return false; 
    }, "Invalid"); 

和目標元素

<input id="org_name" name="org_name" type="text" class="input-xlarge "> 

的addMethod功能不會被調用時,不存在錯誤和我在這一個損失? 另一方面,「必需」驗證在預期的窗體上工作正常,這是我第一次進入這個插件。

+0

請參閱本http://jqueryvalidation.org/jQuery.validator.addMethod/ – krutssss

回答

3

使用規則類似,

org_name: "org_name", 

您的代碼應該是這樣的,

$("#submitform").validate(
    { 
     rules: { 
      org_name: "org_name",// input element: method name 
      email_address: "required", 
      phone: "required", 
      abn: "required", 
      description: "required", 
      geocomplete: "required" 
     }, 
+0

你美麗一工作對待。我曾嘗試過這麼多的變化...... – sapatos