Click to See Complete Forum and Search --> : Validating a grid


Webskater
10-08-2003, 03:17 AM
I have a dynamically created table - so it might have 5 columns and 6 rows or 3 columns and 20 rows. Each cell has a checkbox in it. Before allowing a form to be submitted I need to check that at least one check box in each column and one check box in each row is checked.
At the moment I am looping through the rows checking the status of each checkbox in each column and checking one of them is checked - and then looping through the columns checking at least one checkbox in each row is checked. It works but it seems a big ugly. My checkbox is called 'Assign'. I know how many Assign elements I have, how many rows and how many columns. I think there must be a way of looping through the 'Assign' elements creating arrays based on the column length and/or row length and then doing the checks. Unfortunately, the mind is willing but the brain is feeble. Can anyone point me in the right direction on this? Thanks for any help.

Webskater
10-08-2003, 07:51 AM
In the hope of stimulating a reply .... here is what I have so far.

coltick=0;
rowtick=0;
for (i=1;i<nocols+1;i++)
{
for (j=1;j<table.rows.length-1;j++)
{
if (table.rows[j].cells[i].firstChild.checked==true)
{
coltick = 1
}
}
if (coltick==0)
{
alert('You cannot have a department with no employees. Please assign an employee to the' + table.rows[0].cells[i].innerText + department.');
return false;
}
coltick = 0;
}
for (j=1;j<table.rows.length-1;j++)
{
for (i=1;i<nocols+1;i++)
{
if (table.rows[j].cells[i].firstChild.checked==true)
{ rowtick = 1 }
}
if (rowtick==0)
{
alert('You cannot have an employee without a department. Please assign ' + table.rows[j].cells[0].innerText + 'to a department.');
return false;
}
rowtick=0;
}

So you can see I am looping through the cells and within this loop looping through the rows looking for columns with no ticked checkboxes. And then doing the same thing looping through the rows and within this loop looping through the cells looking for rows with no ticked checkboxes. It works but it seems clunky to me. Can anyone suggest a better way to do this. Thanks.