2009-06-20 77 views
13

我有一個表單,它有一個動態數量的字段組,其中數字基於上一階段的答案。jQuery使用動態數量的字段進行驗證

我生成字段服務器側爲陣列,即

<input id="foo[0]"... 
<input id="bar[0]"... 

<input id="foo[1]"... 
<input id="bar[1]"... 

<input id="foo[2]"... 
<input id="bar[2]"... etc 

不管數量,所有字段都需要&我還需要驗證對型在一些情況下&的位數。我使用jQuery validate plugin進行客戶端處理(是的,也用服務器端的東西備份)&由於表單需要通過XHTML Strict(編輯:請參閱下面的附錄),驗證無法內聯完成。

我的問題是,我不能解決如何使用動態數量的字段驗證。下面介紹一下驗證語法通常如下所示的表格的其餘部分:

$(document).ready(function() { 

    // validate stage_form on keyup and submit 
    var validator = $("#form_id").validate({ 

     // rules for field names 
     rules: { 

      name: "required", 
      address: "required", 
      age: { required: true, number: true } 

     }, 

     // inline error messages for fields above 
     messages: { 

      name: "Please enter your name", 
      address: "Please enter your address", 
      age: { required: "Please enter your age", number: "Please enter a number" } 

     } 

    }); 

}); 

回答

13

實際上它應該工作如果您將使用類而不是初始化規則作爲validate()選項。

標記:

<input id="foo[0]" class="required" 
<input id="bar[0]" class="required number" 

<input id="foo[1]" class="required" 
<input id="bar[1]" class="required email" 

的jQuery:

$(document).ready(function() { 

    var validator = $("#form_id").validate({ 
    messages: { 
      name: "Please enter your name", 
      address: "Please enter your address", 
      age: { 
       required: "Please enter your age", 
       number: "Please enter a number" 
      } 

    } 

    }); 

}); 

希望這個作品。 思南。

+0

這就是我最終做的。看到我的自我回答。 – da5id 2009-09-06 20:56:33

+0

而實際上我會接受你的回答,這樣問題就會被解決。乾杯。 – da5id 2009-09-06 20:57:17

0

沒有答案,所以我會後我的「臨時」的解決方案,這是設置爲「必要」和「類型」在線驗證規則,將'maxlength'留給服務器端檢查,然後用內聯標題標籤顯示自定義消息。

這對於這份工作來說可能夠用了,但我仍然好奇是否有辦法在jQuery中「完全」做到這一點。

2

您是否嘗試過使用custom class rules來定義xhtml不兼容規則?

docs中的示例僅使用一個類,但我想您可以組合不同的自定義類來實現所需的驗證規則。我沒有爲自己嘗試過。

-2

我找到了一種使用「元數據」的方法。

這應該在具有動態名稱的模板中使用。所以,我不需要知道這個名字。

缺點是仍然不可能使用帶純淨標籤的純JavaScript代碼。

<script src="jquery.metadata.js" type="text/javascript"></script> 

<input validate="{required: true, email: true, messages: { required: 'i'm required', 'i'm not valid e-mail' }}" name="dynamicRow[ myRandomNumber ]"type="text" class="input_normal" /> 

$(function() { 
    // setup stuff 
    $.metadata.setType("attr", "validate"); 
}); 
0

這是另一種方法。

/* Normal validate initialisation. */ 
$('#myForm').validate({ 

    /* Use the submitHandler method to add custom-selector-based validation. */ 
    submitHandler: function(form, ev) { 

     /* Find your dynamic field/s. Note that you may want to access them via a scope external to validate, as any selection you do in this internal scope will be static from the form's pre-edit state. */ 
     var el = $('#selector'); 

     /* Do your custom validation. */ 
     if ( el.val() !== 'A' ) { 

      /* Show any errors:- 'fieldname': 'error message'. */ 
      this.showErrors({ 
       'name-of-a-field-near-where-you-want-your-error-placed': 'Please enter "A" to continue' 
      }); 

      /* Prevent form submission. */ 
      return; 
     } 
    } 
});