Very much appreciated if you can help me since I am a newbie on this. I have a huge html table with more than 10 columns and around 20 rows.
I want to do a javascript cycle to verify the inputs of all the rows. My strategy was to insert Ids in each in which each row's id always increases +1. For example:
<td><input id="ingredient_percentage_1" type="text" name=" " form="form1" />%</td>
<td><input id="ingredient_percentage_2" type="text" name=" " form="form2" />%</td>
The code I've done is like this:
function verifyPercentages(){
var total = 0;
var i;
if (batch.value === "") {
alert ("Please insert value in Batch Size field");
return false;
}
else {
for (i=1; i<19; i++){
var percentage = document.getElementById('ingredient_percentage_'+i);
if( (((i>1) && (i<5)) || (i==11) || (i==13)) && (percentage.value == "")) {
alert ("Mandatory fields are not filled up");
return false;
}
if ((i>6) && (i<9) && (percentage.value == "")) {
alert ("Please select an option in the Product type field above");
return false;
}
if ((i>8) && (i<11) && (percentage.value >= 1)) {
alert ("Please make sure that the percentages for oil are less than 1%");
return false;
}
if ((i>14) && (i<17) && (percentage.value >= 1)) {
alert ("Please make sure that (...)");
return false;
}
if (percentage == null) continue;
}
calculatePercentages();
}
}
But I feel that is not very optimized. Is there a more efficient/clean way to do this? Many thanks
