2016-11-25 101 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> 

希望你理解的逻辑,这可能是我犯的错误的地方,以便仔细看..