473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

multiple set of check box validation

16
there are two set of checkboxes in html.one is for foods and other is for books.
if i click on select all option all the check boxes are selected. please show me a validation function which will be called separately to check book and food .
Oct 29 '07 #1
5 1412
epots9
1,351 Expert 1GB
there are two set of checkboxes in html.one is for foods and other is for books.
if i click on select all option all the check boxes are selected. please show me a validation function which will be called separately to check book and food .
Here at TSDN we are more than happy to assist you with your questions but we do ask that you attempt it yourself first. Please try it out and if your code isn't working please provide that section of code and we can help you from there.
Oct 29 '07 #2
samit
16
Here at TSDN we are more than happy to assist you with your questions but we do ask that you attempt it yourself first. Please try it out and if your code isn't working please provide that section of code and we can help you from there.
************************************************** ***************************************
This is my code. I like to call same function for both section to check different section


[HTML]function Checkall(form){
for (var i = 1; i < form.elements.length; i++){

eval("form.elements[" + i + "].checked = true");

}
}

<body>
<form name="frm" method="post" action="" onsubmit="chk(this)">

<input type="checkbox" name="on" onClick="Checkall(frm);" />book<br />

<input type="checkbox" name="c[]" value="1" /> checkbox 1<br />
<input type="checkbox" name="c[]" value="2" /> checkbox 2<br />
<input type="checkbox" name="c[]" value="3" /> checkbox 3<br />
<input type="checkbox" name="c[]" value="4" /> checkbox 4<br />
<input type="checkbox" name="c[]" value="5" /> checkbox 5<br /> <br />
<input type="checkbox" name="un" onClick="Checkall(frm);" />food<br />
<input type="checkbox" name="f[]" value="1" /> checkbox 1<br />
<input type="checkbox" name="f[]" value="2" /> checkbox 2<br />
<input type="checkbox" name="f[]" value="3" /> checkbox 3<br />
<input type="checkbox" name="f[]" value="4" /> checkbox 4<br />
<input type="checkbox" name="f[]" value="5" /> checkbox 5<br />


<!--<input type="submit" value="submit" /> -->
</form>

</body>[/HTML]
Oct 30 '07 #3
acoder
16,027 Expert Mod 8TB
Pass the name of the set of checkboxes to the checkAll function, e.g. "c[]", then use document.getElementsByName:
Expand|Select|Wrap|Line Numbers
  1. function Checkall(chkboxname){
  2.   var chkboxes = document.getElementsByName(chkboxname);
  3.   for (var i = 1; i < chkboxes.length; i++){ 
  4.     chkboxes[i].checked = true;
  5.   }
  6. }  
Oct 30 '07 #4
samit
16
Pass the name of the set of checkboxes to the checkAll function, e.g. "c[]", then use document.getElementsByName:
Expand|Select|Wrap|Line Numbers
  1. function Checkall(chkboxname){
  2.   var chkboxes = document.getElementsByName(chkboxname);
  3.   for (var i = 1; i < chkboxes.length; i++){ 
  4.     chkboxes[i].checked = true;
  5.   }
  6. }  
***************************reply****************** ******************************
given code is working fine to check separate block of check box in the following code. now i like to check at least one of them should be checked else alert will be printed. I have tried many times.but failed using double array. please help.


Expand|Select|Wrap|Line Numbers
  1. function Checkall(chkboxname){
  2.   var chkboxes = document.getElementsByName(chkboxname);
  3.   for (var i = 0; i < chkboxes.length; i++){ 
  4.     chkboxes[i].checked = true;
  5.   }
  6. }
  7.  
[HTML]<body>
<form name="frm" method="post" action="" onsubmit="return chk(this,'<?="c[][]"?>');">

<input type="checkbox" name="on" onClick="Checkall('<?="c[0][]"?>');" />book<br />

<input type="checkbox" name="c[0][]" value="1" /> checkbox 1<br />
<input type="checkbox" name="c[0][]" value="2" /> checkbox 2<br />
<input type="checkbox" name="c[0][]" value="3" /> checkbox 3<br />
<input type="checkbox" name="c[0][]" value="4" /> checkbox 4<br />
<input type="checkbox" name="c[0][]" value="5" /> checkbox 5<br />
<input type="checkbox" name="un" onClick="Checkall('<?="c[1][]"?>');" />food<br />
<input type="checkbox" name="c[1][]" value="1" /> checkbox 1<br />
<input type="checkbox" name="c[1][]" value="2" /> checkbox 2<br />
<input type="checkbox" name="c[1][]" value="3" /> checkbox 3<br />
<input type="checkbox" name="c[1][]" value="4" /> checkbox 4<br />
<input type="checkbox" name="c[1][]" value="5" /> checkbox 5<br />


<input type="submit" value="submit" />
</form>[/HTML]
Nov 1 '07 #5
acoder
16,027 Expert Mod 8TB
You could use something like this function:
Expand|Select|Wrap|Line Numbers
  1. function getNumChecked(chkboxname){
  2.   var chkboxes = document.getElementsByName(chkboxname);
  3.   var numChecked = 0;
  4.   for (var i = 1; i < chkboxes.length; i++){ 
  5.     if (chkboxes[i].checked) numChecked++;
  6.   }
  7.   return numChecked;
  8. }
Then check if the function returns 0.
Nov 1 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: Ken Varn | last post by:
If a page has multiple ValidationSummary controls, how does it distinguish which ValidationControls are associated with which ValidationSummary controls? The reason I am asking this is that I...
1
by: epigram | last post by:
I'm trying to use the ASP.NET validators to check some client-side business rules. I've got two ASP TextBox controls (call them tbxYear1 and tbxYear2) used to enter a range of years. I've got a...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
5
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
2
by: Neo Geshel | last post by:
Greetings, I have a form with a telephone field. It is very specific, as it has four text boxes - the country code, area code, prefix and suffix. I can validate each of them individually, but...
2
by: forbes | last post by:
Hi, I have a form that contains multiple groups of checkboxes that act like radio buttons (at the clients insistance). There is one text field that is required and 28 checkbox groups. Here an...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.