简体   繁体   English

遍历bean列表并验证javascript中的数据

[英]Iterating through a bean list and validate the data in javascript

My form contains input fields which will be validated against minimum and maximum values while submitting.The input fields will be displayed on the screen using list iteration. 我的表单包含输入字段,这些字段将在提交时针对最小值和最大值进行验证。输入字段将使用列表迭代显示在屏幕上。

<c:forEach var="Item" items="${listBean.nameList}" varStatus="status">
    <input type="number"name="nameList<c:outvalue='[${status.index}]'/>.initialWeight" onchange="checkOnChange(this,'<c:out value='${Item.personId}'/>','<c:out value='${Item.minWeight}'/>','<c:out value='${Item.maxWeight}'/>','<c:out value='[${status.index}]'/>')">
        <br><br>
    <input type="number" name="nameList<c:out value='[${status.index}]'/>.finalWeight" onchange="checkOnChange(this,'<c:out value='${Item.personId}'/>','<c:out value='${Item.minWeight}'/>','<c:out value='${Item.maxWeight}'/>','<c:out value='[${status.index}]'/>')">
        <br><br>
    <input type="text" class="formtext" name="nameList<c:out value='[${status.index}]'/>.Reason" id ="reason<c:out value='[${status.index}]'/>" value="" maxlength="255" >
        <br><br>
        <input type="submit" value="submit" id="submit" />

 </c:forEach>

So while submitting the form , i have all the user entered values which will be stored in the bean and also the min/ max values.i need to validate the form and prevent the user from submitting the form if any of the entered value is not within the min / max values. 因此,在提交表单时,我拥有所有用户输入的值(将存储在Bean中)以及最小/最大值。我需要验证表单并防止用户提交表单(如果未输入任何值)在最小值/最大值之内。

So i am a bit of confused on how to do this in Java script ? 所以我对如何在Java脚本中执行此操作感到困惑?

thanks for your suggestions and time .. 感谢您的建议和时间。

JSFIDDLE 的jsfiddle

I would say give an id 我会说出一个id

<form id="frmDetails">
    <c:forEach var="Item" items="${listBean.nameList}" varStatus="status">
        <input type="number"name="nameList<c:outvalue='[${status.index}]'/>.initialWeight" onchange="checkOnChange(this,'<c:out value='${Item.personId}'/>','<c:out value='${Item.minWeight}'/>','<c:out value='${Item.maxWeight}'/>','<c:out value='[${status.index}]'/>')">
            <br><br>
        <input type="number" name="nameList<c:out value='[${status.index}]'/>.finalWeight" onchange="checkOnChange(this,'<c:out value='${Item.personId}'/>','<c:out value='${Item.minWeight}'/>','<c:out value='${Item.maxWeight}'/>','<c:out value='[${status.index}]'/>')">
            <br><br>
        <input type="text" class="formtext" name="nameList<c:out value='[${status.index}]'/>.Reason" id ="reason<c:out value='[${status.index}]'/>" value="" maxlength="255" >
            <br><br>
            <input type="submit" value="submit" id="submit" />

     </c:forEach>
</form>

Below will the form submit function 下面将表格提交功能

<script type="text/javascript">

$("#frmDetails").on("submit",function(e){
var valid=true;
e.preventDefault();
var inputs=$(this).children('input');
$.each('input',function(index,value){
   if($(this).val()=="")//blank validation
   {
          valid=false;
   }
});
if(valid)
{
//post the form
}

$("#frmDetails").unbind("submit"); //To prevent the form from getting submitted 
});

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM