Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FORM VALIDATION - CHECKBOXES: Driving me crazy!!

Status
Not open for further replies.

selaine

Programmer
Oct 11, 2001
85
US
I've been working on this for a week and am a web programmer that kind of learning as I go and I think this is simple, but my brain is fried........

How would I:
1) loop thru form to create array for each set of checkboxes with the same name
2) loop thru each array to make sure at least one checkbox has
been selected from each group - if not ... alert "Must
select at leach one checkbox from each Category"

Maybe create a validation function?:
whichCheckBoxArray = the Name of the checkbox to be checked, a string
myMin = 1 (the least you want the user to be able to check, an integer)


for all elements in form of type checkbox, named (var), make sure at
least one checkbox is selected

run function on submit......then proceed to next page if all is well

example sets:
<form action=&quot;add_selections.asp&quot; method=&quot;post&quot; name=&quot;subcategory&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4201&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4202&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4203&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4204&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4205&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4206&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4207&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4208&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4209&quot;>

<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4401&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4402&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4403&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4404&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4405&quot;>
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4406&quot;>

<input type=&quot;submit&quot; value=&quot;Submit Form&quot;><input type=&quot;Reset&quot; value=&quot;Reset Form&quot;>
</form>
 
>> loop thru form to create array for each set of
>> checkboxes with the same name

You don't have to do that. They already are an array since they have the same name.
Code:
<SCRIPT LANGUAGE=javascript>
<!--
function testing(){
	alert(document.subcategory.cksub4200.length);
	for(cxb=0; cxb<document.subcategory.cksub4200.length; cxb++)
		alert(document.subcategory.cksub4200(cxb).checked);
}
//-->
</SCRIPT>

To know what names are in the form just write a javascript array of those names into your function from the server side code. That part is easy since the server code know what names it created right?

-pete
 
I guess I forgot a crucial detail..... Sorry.....

The checkbox set are created dynamically, so I the only thing I know in advance is all of the possible names used in the sets. On this particular form page, there may be 1 set or there may be more sets, it's all dependant on the selections from a form on the previous page that contain a set of checkboxes.......

How would I handle that scenario, vs. knowing the names and number of sets ahead of time?
 
generate the JS code along with ur checkbox.

it will look like this:
Code:
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4201&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4202&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4203&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4204&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4205&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4206&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4207&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4208&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4200&quot; value=4209&quot;>
<script>
<!--
function testing(){
    alert(document.subcategory.cksub4200.length);
    for(cxb=0; cxb<document.subcategory.cksub4200.length; cxb++)
        alert(document.subcategory.cksub4200(cxb).checked);
}
//-->

</script>            

<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4401&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4402&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4403&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4404&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4405&quot;>    
<input type=&quot;checkbox&quot; name=&quot;cksub4400&quot; value=4406&quot;>
<script>
<!--
function testing1(){
    alert(document.subcategory.cksub4400.length);
    for(cxb=0; cxb<document.subcategory.cksub4400.length; cxb++)
        alert(document.subcategory.cksub4400(cxb).checked);
}
//-->

</script>

this is just to give u an idea...


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top