2012-09-04 131 views
1

我正在使用jQuery驗證在提交前檢查我的表單。在我的HTML中,我有類似名稱的輸入,ids。我想知道如何驗證具有相似名稱的輸入,而不必逐個寫出(類似於jQuery選擇具有相似名稱的元素)?jQuery驗證插件驗證具有相似名稱的輸入?

HTML 
<form id="form1"> 
<table> 
<tr><th>Input 1:</th><td><input type="text" name="input1" id="id_input1"/></td></tr> 
<tr><th>Input 2:</th><td><input type="text" name="input2" id="id_input2"/></td></tr> 
<tr><th>Input 3:</th><td><input type="text" name="input3" id="id_input3"/></td></tr> 
</table> 
</form> 

Javascript 

$("#form1").validate({ 
submitHandler:function(form) { 
SubmittingForm() 
}, 
rules: {     //I am thinking if there has similar stuffs like 
          //$('th[name^="input"]') 
input1: "required",  
input2:"required", 
input3:"required",      
    }, 
messages: { 
    input1: "Please finish the table.",     
    input2: "Please finish the table.", 
     input3: "Please finish the table." 
      } 
}) 

回答

1

對於驗證部分,您只能向文本字段添加class =「required」,並且它會自動驗證它。

至於這些消息,你可以參考這個post

+0

感謝您的答覆。但在我真實的代碼中,我已經有了一個充滿其他東西的類名。 –

+0

你可以有超過1班......所以你仍然可以添加class =「任何要求」 –