codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  asp: FORM VALIDATION - CHECKBOXES: Driving me crazy!!!  seala at 22:30 on Wednesday, September 10, 2003
 

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?

  Re: asp: FORM VALIDATION - CHECKBOXES: Driving me crazy!!!  Troy Wolf at 01:03 on Thursday, September 11, 2003
 

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>
Troy Wolf: site expert
Shiny Solutions









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  XML- Passing parameters are Latitude and Longitude.
•  Re: Format Date time issue....
•  Re: How to freeze the web page when displaying progress bar?
•  Re: Help! IFrames!
•  Re: Help: Trouble with z-Index and SELECT lists
•  Re: HOW CREATE LINE CHARTS IN ASP.NET
•  How to store and retrieve document into database
•  Re: text field multiple
•  Re: after updation of a table open another program .. need help


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005