|
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="/forum/add_selections.html" method="post" name="subcategory">
<input type="checkbox" name="cksub4200" value=4201">
<input type="checkbox" name="cksub4200" value=4202">
<input type="checkbox" name="cksub4200" value=4203">
<input type="checkbox" name="cksub4200" value=4204">
<input type="checkbox" name="cksub4200" value=4205">
<input type="checkbox" name="cksub4200" value=4206">
<input type="checkbox" name="cksub4200" value=4207">
<input type="checkbox" name="cksub4200" value=4208">
<input type="checkbox" name="cksub4200" value=4209">
<input type="checkbox" name="cksub4400" value=4401">
<input type="checkbox" name="cksub4400" value=4402">
<input type="checkbox" name="cksub4400" value=4403">
<input type="checkbox" name="cksub4400" value=4404">
<input type="checkbox" name="cksub4400" value=4405">
<input type="checkbox" name="cksub4400" value=4406">
<input type="submit" value="Submit Form"><input type="Reset" value="Reset Form">
</form>
<Added>
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?
|
|
|
Here you go. When you click Submit, the Validate() function runs which calls a function called LimitChecks() twice--once for each checkbox group. LimitChecks() loops through the specified checkbox group and does its thing. Enjoy!
Copy this code to a new HTML page, then run it to see it in action.
<script language=javascript>
function LimitChecks(groupTitle,elementName,allowedQty)
{
var f = document.forms[0];
var e = f.elements[elementName];
var checkedQty = 0;
for(idx=0;idx<e.length;idx++)
{
if(e[idx].checked)
{
checkedQty++;
}
}
if(checkedQty > allowedQty)
{
alert("You checked "+checkedQty+
" checkboxes in the\n"+
groupTitle+" section,\n"+
"but only "+allowedQty+
" are allowed.\n\n"+
"Please uncheck some checkboxes.");
}
}
function Validate()
{
LimitChecks("4200 Checkboxes","cksub4200",3);
LimitChecks("4400 Checkboxes","cksub4400",2);
}
</script>
<form action="/forum/add_selections.html" method="post" name="subcategory" ID="Form1">
<input type="checkbox" name="cksub4200" value=4201>
<input type="checkbox" name="cksub4200" value=4202>
<input type="checkbox" name="cksub4200" value=4203>
<input type="checkbox" name="cksub4200" value=4204>
<input type="checkbox" name="cksub4200" value=4205>
<input type="checkbox" name="cksub4200" value=4206>
<input type="checkbox" name="cksub4200" value=4207>
<input type="checkbox" name="cksub4200" value=4208>
<input type="checkbox" name="cksub4200" value=4209>
<p>
<input type="checkbox" name="cksub4400" value=4401>
<input type="checkbox" name="cksub4400" value=4402>
<input type="checkbox" name="cksub4400" value=4403>
<input type="checkbox" name="cksub4400" value=4404>
<input type="checkbox" name="cksub4400" value=4405>
<input type="checkbox" name="cksub4400" value=4406>
<p>
<input type="button" value="Submit Form" onclick="Validate()">
<input type="Reset" value="Reset Form">
</form>
|
|
|
|
|
|
|
|
|
|
|
|