I am making a form to order items stored in a database. Each item has an input field for the number of items that are to be ordered. The code for each item looks like this.
<div class="col-md-5">
<label for="item13">409 Carpet Cleaner</label>
</div>
<div class="col-md-2" div="">
<div class="has-success has-feedback">
<input type="hidden" name="itemID[]" value="13">
<input type="hidden" name="metaID[]" value="73">
<input type="number" class="form-control" id="item13" name="quantity[]" min="0" max="10" value="0">
<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
<span id="inputSuccess2Status" class="sr-only">(success)</span>
</div>
</div>
<div class="col-md-5">
<span class="text-success"> X 1 case (12 aerosol cans)</span>
</div>
That HTML just gets added and repeated to the page dynamically from my database by PHP depending on how many items there are. (Right now they 113 items in my form, since why I'm using arrays in the name attribute)
My submit button is like this:
<button type="button" class="btn btn-lg btn-success pull-right" id="submitOrder">Submit Order For Entire Store Location</button>
And my validation script is as follows:
$("input[name='quantity[]']").on("keyup", function() {
var value = $(this).val();
var maxValue = $(this).attr('max');
var minValue = $(this).attr('min');
var itemID = $(this).attr('id');
maxValue = parseInt(maxValue);
minValue = parseInt(minValue);
value = parseInt(value);
//console.log("we was called " + minValue + " " + value + " " + maxValue + " " + itemID);
if(value > maxValue || value < minValue){
//console.log("wrong number");
$("#submitOrder").attr("disabled", "disabled");
$('#' + itemID).css("background-color","#f36e65");
}else{
$("#submitOrder").removeAttr("disabled");
$('#' + itemID).css("background-color","#43C56D");
}
});
So what I want to do is that if any input field fail to meet the validation, to keep the submit button disabled. Right now it disables it if an input fails but if another similar input field is typed in after that and meets the validation then the submit button is enabled, even though the previous input failed it.
How can I keep track of the errors and disable the button if there are any and enable it once they're fixed.?