0

My form is loaded with different input fields like radio button , text field ,numeric field which are generated dynamically while iterating through a list.

<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>

The numeric fields will be validated against minimum and maximum values.if any of the numeric fields fails in the validation , submit button needs to be disabled .

JSFIDDLE

Any ways to achieve this using jquery or javascript?

Thanks for your valuable time and suggestions

3
  • @LShetty Thanks for your reply.But how will that make any difference for this scenario? Commented Apr 17, 2015 at 13:17
  • JavaScript does not care about your framework. Show us only the rendered HTML of the form. Commented Apr 17, 2015 at 15:02
  • Also do not tag-spam. Only use the jquery-validate tag if the question is about this specific plugin and the jquery-plugins tag if the question is about jQuery plugins in general. Please do not rely solely on the jsFiddle to show the code; it must also be included in the OP in case the link goes dead. Thanks. Commented Apr 17, 2015 at 15:05

2 Answers 2

1

Give IDs to your inputs and then use jQuery's .change() function.

Example:

HTML

<input id="test" type="number"/>
<input id="test2" type="number"/>
<input id="submit" type="submit" value="Submit"/>

JS

var testVal = $('#test'),
    testVal2 = $('#test2'),
    submit = $('#submit'),
    minVal = 22,
    maxVal = 33;

testVal.change(function() {
    if((testVal.val() > maxVal || testVal.val() < minVal) ||
       (testVal2.val() > maxVal || testVal.val() < minVal)) {
        submit.prop('disabled', true);
    } else {
        submit.prop('disabled', false);
    }
});

jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

0

So, you could do something like below.

var flag = true;
if (nameVal > maxVal) {
    alert(id + ' Enter the reason before proceeding');
    //var reason = nameList[index].Reason;
    document.getElementbyId("reason" + index).focus();
    flag = false;
}
if (itemVal < minVal) {
    alert(id + ' Enter the reason before proceeding');
    //var reason = nameList[index].Reason;
    document.getElementbyId("reason" + index).focus();
    flag = false;
}

document.getElementById("submit").disabled = !flag;
return flag;

Comments

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.