2016-11-25 102 views
5

我有多個形式是這樣的:多形式的動態領域JavaScript驗證

<?php for ($i = 0; $i > $n; $i++) { ?> // n is no. of value (no limit) 
    <form> 
     <input name="<?php echo $i; ?>venue" type="text"> 
     <input name="roster" type="text"> 
     <input type="submit" name="btn_venue"> 

    </form> 
<?php } ?> 
    <form> 
     <input name="hospitality" type="text"> 
     <input name="template" type="text"> 
     <input type="submit" name="btn_hospitality"> 
    </form> 
    ... 
    ... 
    <form> 
     <input name="element" type="text"> 
     <input name="alignment" type="text"> 
     <input type="submit" name="btn_xyz"> 
    </form> 

我想驗證(場不應該是空白)在所有形式的話,我該如何使用驗證?

我試圖jQuery驗證:

<script> 
    $('document').ready(function() { 
     $('form').each(function (key, form) { 
      $(form).validate({ 
       rules: { 
        hospitality: { 
         required: true 
        }, 
        //... 
        //... 
        alignment: { 
         required: true 
        } 
      }); 
     }); 
    }); 
</script> 

我有管理靜態名稱字段驗證,但我不知道有關動態venue名主意validation.Can誰能幫助我?

如果只有一個表單容易維護但動態多表單提交驗證如何驗證。

一次只有一個表單提交,但特定的表單字段驗證它是如何可能的?

+1

如何提交表單?你沒有提交按鈕..? –

+0

@StefanMichelangeloMihajlovic更新代碼請檢查它。 –

+0

在需要的輸入中添加「required」屬性,支持HTML5的瀏覽器將自動對其進行驗證。如果你想要自定義錯誤信息,你可以使用Parsley.js – rogeriolino

回答

2

試試這個。它貫穿每個表單,並通過每個輸入(文本類型)對每個表單進行檢查,併爲每個輸入建立必需的規則。然後將動態構建的規則提供給驗證函數。

$('document').ready(function() { 
    $('form').each(function (key, form) { 
     // build the rules object dynamically 
     var rules = {}; 
     // loop through each input in the form 
     $(form).find('input[type="text"]').each(function(idx, obj) { 
      // make a rule for this input 
      rules[obj.name] = {"required": true}; 
     }); 
     $(form).validate({ 
      "rules": rules 
     }); 
    }); 
}); 
1

Okey這不是確定最乾淨的方式來做到這一點,但它的第一個跨越我的想法。 首先,編輯你的html,所以表格有id="form$i",輸入有id="input$i",並提交了id="$i"。同時添加類提交class="validate"

<?php for ($i = 0; $i > $n; $i++) { ?> // n is no. of value (no limit) 
    <form id="form<?php echo $i; ?>">//so id="form2" for example 
     <input id="input<?php echo $i; ?>" name="<?php echo i; ?>venue" type="text">// id="input2" 
     <input name="roster" type="text"> 
     <input id="<?php echo $i; ?>" class="validate" type="submit" name="btn_venue">//id="2" 
    </form> 
<?php } ?> 

JQuery的應該工作,我會解釋每一行代碼

<script> 
    $(function() { 
     $(".validate").click(function() { //this is call for each click button with class validate 
      var id = $(this).attr('id');//getting id of the clicked element which is $i 
      var formid = 'form' + id;//creating new var formid which will target formid (for example: in first loop $i=1, so id=1, and formid=form1) 
      var input = 'input' + id;//creating input which value will be input$i 
      $('#' + formid).on('submit', function (e) {//on form submit validate function 
       if ($(#input).value == '') { 
        return false; 
       } else { 
        return true; 
       } 
      }); 
     }); 
    }); 
</script> 

希望你理解的邏輯,這可能是我犯的錯誤的地方,以便仔細看..