Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checking 1 of 3 checkboxes?

    First off, great forum - I have found many answers here so far, but something eludes me....

    I have a form with 3 checkboxes per question because I want to allow a user to select more than one response, however I want to make sure they check atleast 1 of each of the 3 responses. For instance:

    <form name="formcheck">
    <input type="checkbox" name="C1" value="ON">
    <input type="checkbox" name="C2" value="ON">
    <input type="checkbox" name="C3" value="ON">

    <input type="checkbox" name="C4" value="ON">
    <input type="checkbox" name="C5" value="ON">
    <input type="checkbox" name="C6" value="ON">

    <input type="checkbox" name="C7" value="ON">
    <input type="checkbox" name="C8" value="ON">
    <input type="checkbox" name="C9" value="ON">
    </form>

    So they should check atleast 1 out of C1, C2, C3, one of C4, C5, C6, etc,etc. There is a total of 45 checkboxes (up to C45).

    if anyone can help me with a form validation code that can do this i would appreciate it

    thanks
    dentaldude

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    You can try this I didn't test it but it might work:

    Code:
    function validateForm()
    {
    count1=3;
    count2=0

    for(i=0;i<count1;i++)
    {
    wuz_checked=document.formcheck.element[i].checked;

    if(wuz_checked==true)
    {
    count1=count1+3;
    count2=count2+3;
    i=count2;
    }
    else
    {
    alert("Please Check At Least One Box Per Category");
    }
    if(i>formcheck.length){
    return;
    }
    }
    }
    Last edited by Basscyst; 10-10-2003 at 06:54 AM.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Northern California
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This works!

    Code:
    <html>
    <head>
    	<script>
    	function isChecked(eles){
    	var e=eles.split(',');
    	var isChk=false;
    		for(var i=0;i<e.length;i++){
    			if(document.getElementById(e[i]).checked){
    				isChk=true;
    				break;
    			}
    		}
    		if(!isChk) alert("you must make atleast one selection");
    		
    		return isChk;
    	}
    	</script>
    </head>
    <body>
    <form name="formcheck1" onsubmit="isChecked('C1,C2,C3');">
    <input type="checkbox" name="C1" value="ON">
    <input type="checkbox" name="C2" value="ON">
    <input type="checkbox" name="C3" value="ON">
    <br><input type="submit" value="Check Group 1">
    </form>
    
    <form name="formcheck2" onsubmit="isChecked('C4,C5,C6');">
    <input type="checkbox" name="C4" value="ON">
    <input type="checkbox" name="C5" value="ON">
    <input type="checkbox" name="C6" value="ON">
    <br><input type="submit" value="Check Group 2">
    </form>
    
    <form name="formcheck3" onsubmit="isChecked('C7,C8,C9');">
    <input type="checkbox" name="C7" value="ON">
    <input type="checkbox" name="C8" value="ON">
    <input type="checkbox" name="C9" value="ON">
    <br><input type="submit" value="Check Group 3">
    </form>
    
    
    </form>
    </body>
    </html>
    Last edited by scriptkeeper; 10-10-2003 at 07:28 AM.

  • #4
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That does work and thanks, but I don't want seperate submit buttons for each question, it would be easier to have 1 submit button for the whole form, follow?

    is there a way to modify this script to account for that?

    thanks in advance

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    i dont have time to make something better, but this works. I relies on a grouping attribute in the checkbox tags
    Code:
    <html>
      <head>
        <script>
          function checkit (objForm) {
            var group;
            var previousgroup = 0;
            var thisgroupcheck = true;
            for (var i = 0; i < objForm.elements.length; i++){
              var elem = objForm.elements[i]
              if (elem.type == "checkbox") {
                 group = elem.group;
                 if (group != previousgroup) {
                    if (!thisgroupcheck) {
                      alert ("group " + previousgroup + " has no check");
                    }
                    previousgroup = group;
                    thisgroupcheck = false;
                 }
                 if (elem.checked || thisgroupcheck) {
                   thisgroupcheck = true;
                 }
              }
            }
            // verify the last group
            if (!thisgroupcheck) {
              alert ("group " + previousgroup + " has no check");
            }
          }
        </script>
      </head>
    
    
      <body>
        <form name="formcheck">
          <input type="checkbox" name="C1" value="ON" group="1">
          <input type="checkbox" name="C2" value="ON" group="1">
          <input type="checkbox" name="C3" value="ON" group="1">
          <br>
          <input type="checkbox" name="C4" value="ON" group="2">
          <input type="checkbox" name="C5" value="ON" group="2">
          <input type="checkbox" name="C6" value="ON" group="2">
          <br>
          <input type="checkbox" name="C7" value="ON" group="3">
          <input type="checkbox" name="C8" value="ON" group="3">
          <input type="checkbox" name="C9" value="ON" group="3">
          <br>
          <input type="button" onclick="checkit(this.form);">
        </form>
      </body>
    </html>
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •