Forum Moderators: open
i.e.
weight1
weight2
weight3
etc.
i need to figure out how to obtain the total value of all the fields, before submitting the form.
my feeble attempts were variations of this:
var n = 0;
var i = 0;
var x = 0;
var form = "form100";
var prefix = "weight";
while (form[prefix + i++]) {
x = form[prefix + i++].value;
n = n + x;
}
then check the value of n. can anyone help?
-Matt
// this function checks the values of the dynamically generated fields.
// the prefix is the field name i.e. <input type = "text" name = "weight<%=count%>">
// the Check100 function adds up all entered values in the fields, and makes sure the total value is 100. if it's not 100, throw error.
function Check100() {
var n = 0;
var i = 0;
var x = 0;
var prefix = "weight";
while (++i <= <%=n%>) { // this n variable is an ASP variable pulled from a form
x = (document.checkform[prefix + i].value) - 0; // subtracting zero from a string, converts it to type integer
n = eval(n + x); // keep track of cumulative total
}
if (n!= 100) {
alert('Your values must equal 100');
return false;
}
else {
return true;
}
}
-Matt
yeah, i figured the eval() doesn't hurt anything, so i kept in there. like you said, it's just added insurance.
-Matt