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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    CheckBox Validation

    Anyone know how I can make sure that out of a group of 10 checkboxes atleast 1 is checked?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I did try this but it doesn't seem to work as an or statement both of them have to be checked for the JavaScript to pass:

    Code:
    if (!document.form.check111.checked || !document.form.check112.checked) {
    theMessage = theMessage + "\n --> Q10 Please select atleast 1 topic";
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    If you used the search feature you would see that this has been covered many times before.

    Example:-


    <form name="myform" method="POST" action="" onsubmit="return checkTheBox();">
    <input type="checkbox" name="check[]" value="1" /> 1 &nbsp;&nbsp;
    <input type="checkbox" name="check[]" value="2" /> 2 &nbsp;&nbsp;
    <input type="checkbox" name="check[]" value="3" /> 3 &nbsp;&nbsp;
    <input type="checkbox" name="check[]" value="4" /> 4 &nbsp;&nbsp;
    <input type="checkbox" name="check[]" value="5" /> 5 &nbsp;&nbsp;
    <input type="submit" value="Submit Form" />
    </form>

    <script type = "text/javascript">
    function checkTheBox() {
    var flag = 0;
    for (var i = 0; i< 5; i++) {
    if(document.myform["check[]"][i].checked){
    flag ++;
    }
    }
    if (flag == 0) {
    alert ("You must check at least one checkbox!");
    return false;
    }
    return true;
    }
    </script>


  •  

    Posting Permissions

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