Validate undefined number of checkboxes and textfields

J

JAPIO

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?
 
J

Joel Byrd

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) {
if (form_name.gecontroleerdoor == "") {
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.
 
L

Lee

JAPIO said:
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?

Your PHP script will know how many of each element there are before
it's finished writing the page. Let it generate the validation script.
 
J

JAPIO

Currently i have the function below.

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

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

for ( i=0 ; i < aantaldwgs ; i++ )
{
if ( f.goedgekeurd.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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top