From: amerar on
Hi All,

I have a form generated from a Perl script. The number of check boxes
on the form is unknown until the time the form is generated. The
names of the check boxes are created from some values in the database,
based upon other user input.......<crazy, I know>

The user must choose at least one check box.

So, how can I write a javascript routine that will perform checking on
an unknown number of check boxes.

Thanks,

Arthur

From: mick white on
amerar(a)iwc.net wrote:

> Hi All,
>
> I have a form generated from a Perl script. The number of check boxes
> on the form is unknown until the time the form is generated. The
> names of the check boxes are created from some values in the database,
> based upon other user input.......<crazy, I know>
>
> The user must choose at least one check box.
>
> So, how can I write a javascript routine that will perform checking on
> an unknown number of check boxes.
>
/*
By creating a function that creates a collection of checkboxes, then
looping through their "checked" properties. If any of the checked
properties is true, return true. If not, return false.
If there is only one form in the document:
*/

function isOneCheckboxChecked(form){
var f=form.length;
while(f--){
if(form[f].type=="checkbox" && form[f].checked){
return true;
}
}
return false;
}

(Not tested)
Mick






From: amerar on

Well, the issue is this: I create an unknown number of check boxes.
Their name comes from a database, so they do not have a simple name
like, check1, check2, etc......

The form is then sent to the user. When the user submits the form, I
want to do some Javascript checking to make sure that they selected at
least one checkbox.........

How to do that if I do not know how many boxes there are, or the
names???

From: mick white on
amerar(a)iwc.net wrote:

> Well, the issue is this: I create an unknown number of check boxes.
> Their name comes from a database, so they do not have a simple name
> like, check1, check2, etc......
>
> The form is then sent to the user. When the user submits the form, I
> want to do some Javascript checking to make sure that they selected at
> least one checkbox.........
>
> How to do that if I do not know how many boxes there are, or the
> names???
>
<script type ="text/javascript">
function isOneCheckboxChecked(form){
var f=form.length;
while(f--){
if(form[f].type=="checkbox" && form[f].checked){
return true;
}
}
alert("OOPS");
return false;
}
</script>

<form
action="javascript:alert('OK');"
onsubmit="return isOneCheckboxChecked(this);">


No need to know how many checkboxes there are, or know their names, or
their religious persuasions...

Mick.






From: amerar on

Ahh, sorry, did not read the code all the way through. I'll give it a
whirl.....thanks.