need help requiring at least one checkbox from group to be selected

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 33
Reputation: edmicman is an unknown quantity at this point 
Solved Threads: 0
edmicman's Avatar
edmicman edmicman is offline Offline
Light Poster

need help requiring at least one checkbox from group to be selected

 
0
  #1
Sep 12th, 2007
I'm using classic ASP here, and trying to do some Javascript validation.

In my form, I'm dynamically creating checkbox groups - each group has a common Name, and each checkbox has a unique ID. Before the form submits (in the onclick on the submit button) I want to check and make sure each checkbox group has at least one of it's checkboxes checked. If that makes sense :-)

I guess I'm conceptually running into problems. I think I want to, on submit, loop through all of the checkbox Names, and make sure at least one item is checked out of each of those Name groups. Can this be done? This is what I have so far, but I think it's getting thrown off by using the ID instead of the Name:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function validateCheckBox()
  2. {
  3. var checkSelected = false;
  4. var checkboxid
  5.  
  6. for(i=0; i<document.frmEditCoverages.elements.length; i++)
  7. {
  8. if(document.frmEditCoverages.elements[i].type=="checkbox")
  9. {
  10. /*need to do this for each checkbox on the page*/
  11. checkboxid = document.frmEditCoverages.elements[i];
  12. for (i = 0; i < checkboxid.length; i++){
  13. if (checkboxid[i].checked){
  14. checkSelected = true;
  15. }
  16. }
  17. if (!checkSelected){
  18. alert("WARNING MESSAGE TELLING THEM TO CHECK A CHECKBOX.");
  19. return (false);
  20. }
  21. }
  22. }
  23.  
  24. }

Any help or advice? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: need help requiring at least one checkbox from group to be selected

 
0
  #2
Sep 13th, 2007
Shouldn't you return a value of true if a box is selected?

Also, you are returning before you check all of the boxes.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 33
Reputation: edmicman is an unknown quantity at this point 
Solved Threads: 0
edmicman's Avatar
edmicman edmicman is offline Offline
Light Poster

Re: need help requiring at least one checkbox from group to be selected

 
0
  #3
Sep 14th, 2007
Well, yeah, I probably need to return checkSelected or something. Errrrr, actually, it probably would work if it popped the warning for each one, and then returned for that one. Ideally I'd focus on that element so they can select it. But shouldn't the FOR loop through all the elements hit all the checkboxes?

I guess what I want to do is loop through all of the checkboxes on the page, but name instead of ID so I can get the groups of checkboxes. And if at least one box isn't checked for each group, I want to pop the message. Sigh....I shouldn't be struggling with this as much as I am I'll try breaking it down some more and see what I can do...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,636
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 470
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: need help requiring at least one checkbox from group to be selected

 
0
  #4
Sep 14th, 2007
You need to have a basic understanding of Javascript objects for understanding the solution presented below:

Test.html
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  5. <link rel = "stylesheet" href = "Styles.css" media = "all" type = "text/css" />
  6. <title>sample page</title>
  7. </head>
  8. <body id = "myBody">
  9. <noscript>Your browser doesn't support javascript. Be prepared for broken site functionality...</noscript>
  10. <form id = "myForm" action = "">
  11. <div id = "myDiv">
  12. <fieldset>
  13. <legend>First</legend>
  14. <input type = "checkbox" name = "first" id = "first" value = "1" /><label for = "first">One</label><br />
  15. <input type = "checkbox" name = "first" id = "second" value = "2" /><label for = "second">Two</label><br />
  16. <input type = "checkbox" name = "first" id = "third" value = "3" /><label for = "third">Three</label><br />
  17. </fieldset>
  18. <br /><br />
  19.  
  20. <fieldset>
  21. <legend>Second</legend>
  22. <input type = "checkbox" name = "second" id = "four" value = "4" /><label for = "four">Four</label><br />
  23. <input type = "checkbox" name = "second" id = "five" value = "5" /><label for = "five">Five</label><br />
  24. </fieldset>
  25. <br /><br />
  26.  
  27. <fieldset>
  28. <legend>Third</legend>
  29. <input type = "checkbox" name = "third" id = "six" value = "6" /><label for = "six">Six</label><br />
  30. <input type = "checkbox" name = "third" id = "seven" value = "7" /><label for = "seven">Seven</label><br />
  31. </fieldset>
  32. <br /><br />
  33. </div>
  34. <input type = "submit" value = "Check validity" id = "btn" onclick = "SOS.validate('myDiv');" />
  35. </form>
  36. <script type = "text/javascript" src = "Lib.js"></script>
  37. </body>
  38. </html>
  39.  

Libs.js
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var SOS =
  2. {
  3. validate : function(id)
  4. {
  5. var obj = {};
  6. var element = null;
  7. var myDiv = document.getElementById(id);
  8. var elements = myDiv.getElementsByTagName('input');
  9.  
  10. for(var i = 0, limit = elements.length; i < limit; ++i)
  11. {
  12. element = elements[i];
  13. obj[element.name] = !!(obj[element.name] || element.checked);
  14. }
  15.  
  16. for(var prop in obj)
  17. {
  18. if(obj[prop] == false)
  19. {
  20. alert('Check at least one box in the group ' + prop.toUpperCase());
  21. return(false);
  22. }
  23. }
  24. alert('Form successfully submitted');
  25. return(true);
  26. }
  27. }

Styles.css
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. #myDiv
  2. {
  3. width: 20%;
  4. }
  5. #myDiv legend
  6. {
  7. color: #ababab;
  8. font-weight: bold;
  9. }
  10.  
  11. #myDiv fieldset
  12. {
  13. background-color: #efefef;
  14. }
  15.  
  16. #btn
  17. {
  18. border: 1px #aabbcc solid;
  19. }
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC