2017-08-16 51 views
0
<?php 
    <input type='text' name='contact_number[$id]' id='contact_number'> 
?> 

其中,我發佈多個聯繫人數組中。我想在聯繫人中添加jquery驗證,並且它必須是真實的。但它不起作用。如何在多個字段上使用jquery驗證?

我jQuery代碼是:

$("#editUserForm").validate({ 
    rules: { 
     "contact_number": { 
      required: true, 
      minlength: 10, 
      maxlength: 10, 
      number: true 
     }  
    }, 
    messages: {  
    } 
}); 
+0

是相同還是輸入名稱相同 – whoami

+0

輸入名稱相同,並且將具有不同的ID。即多輸入。 – kohinoor

回答

0

"contact_number"永遠比不上contact_number[],或任何其他版本的數組索引。這個插件不能像那樣工作。

,您可以指定每一個內.validate()究竟...

$("#editUserForm").validate({ 
    rules: { 
     "contact_number[1]": { 
      required: true, 
      minlength: 10, 
      maxlength: 10, 
      number: true 
     }, 
     "contact_number[2]": { 
      // rules 
     } 
     .... 

或動態匹配「開始」 contact_number使用.rules()方法的所有領域。

$('[name^="contact_number"]').each(function() { 
    $(this).rules('add', { 
     required: true, 
     minlength: 10, 
     maxlength: 10, 
     number: true 
    }); 
});