Connecting Tech Pros Worldwide Forums | Help | Site Map

Validate undefined number of checkboxes and textfields

JAPIO
Guest
 
Posts: n/a
#1: Nov 23 '05
I have a form which is generated by PHP. This form contains one
textbox, one textarea and one checkbox all at one row. This row is
being repeated multiple times according to the number of results from a
database.

gecontroleerdoor[]
opmerkingen[]
goedgekeurd[]

The thing i want to do is to check if all the textboxes have a value
and that all the checkboxes are checked with true or false as outcome.

Can someone help me out with this?


Joel Byrd
Guest
 
Posts: n/a
#2: Nov 23 '05

re: Validate undefined number of checkboxes and textfields


If the array indexes are just integers that are being incremented
(gecontroleerdoor[0], gecontroleerdoor[1], etc...), then you could use
something like the following (javascript):

var all_have_values = true;
var i = 0;

while (form_name.gecontroleerdoor[i]) {
if (form_name.gecontroleerdoor[i] == "") {
var all_have_values = false;
break;
}
i++;
}

This example is assuming that gecontroleerdoor[] is the textbox array
and you want to check to see that all these textboxes have values (are
not empty). You can apply similar code to the other form elements.
The syntax may not be perfect, but I think this should basically work.

JAPIO
Guest
 
Posts: n/a
#3: Nov 23 '05

re: Validate undefined number of checkboxes and textfields


Currently i have the function below.

function checkCheckboxes(f)
{
var numberofrows = <?= $numberofrows?>;
var afgekeurd = 0;

for ( i=0 ; i < numberofrows; i++ )
{
if ( f.gecontroleerddoor[i].value == '' ) afgekeurd++;
}

for ( i=0 ; i < aantaldwgs ; i++ )
{
if ( f.goedgekeurd[i].checked == false ) afgekeurd++;
}
}

But this produces the following error in FireFox:
Error: f.gecontroleerddoor has no properties

Which refers tot the following line:
if ( f.gecontroleerddoor[i].value == '' ) afgekeurd++;

The form i used for the input is created by the following code:

foreach( $batchdwgs as $batchdwg )
{
echo "<tr><td valign=\"top\">" . $batchdwg->batchdwg_naam . "<input
name=\"batchdwgid[]\" type=\"hidden\" value=\"" .
$batchdwg->batchdwg_id . "\" /></td>
<td valign=\"top\"><input name=\"gecontroleerddoor[]\" type=\"text\"
value=\"" . $batchdwg->batchdwg_gecontroleerddoor . "\"></td>
<td valign=\"top\"><textarea name=\"opmerkingen[]\" cols=\"30\"
rows=\"2\">" . $batchdwg->batchdwg_opmerkingen . "</textarea></td>
<td align=\"center\"><input name=\"goedgekeurd[]\" type=\"checkbox\"
value=\"1\"" . (( $batchdwg->batchdwg_goedgekeurd )?" CHECKED":"") . "
onChange=\"checkCheckboxes(this.form);\"></td>
</tr>";
$i++;
}

I'm wondering what i'm doing wrong.

Closed Thread