By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Validating a checkbox......

P: n/a
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

Jan 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
am****@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


Jan 12 '06 #2

P: n/a

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

Jan 13 '06 #3

P: n/a
am****@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.


Jan 13 '06 #4

P: n/a

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

Jan 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.