0

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.

So i am a bit of confused on how to do this in Java script ?

thanks for your suggestions and time ..

JSFIDDLE

1 Answer 1

1

I would say give an 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>
Sign up to request clarification or add additional context in comments.

3 Comments

You should use e.preventDefault() only when the form is not valid.
@Guruprasad Rao Thanks for your reply.But in my case i need to pass the min and maximum values stored in the bean to the jquery function so that it can be validated.But your answer merely checks for blank values
Actually I've no experience or better to say do not know about this bean list, so I just gave you a basic idea of how you can validate... :) Anyhow this link might be helpful.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.