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

    Making Sure Checkboxes Are Checked

    I have 3 checkboxes in a form... The 1st checkbox can only be checked if the 3rd checkbox has also been checked... So basically checkbox1 or 2 HAS to be checked, but 1 can ONLY be checked if 3 is checked... Whoa confuse much!

    I have the following JavaScript it's from a much larger part of a form I am doing so need to edit it to fit... At the moment obvioulsy it only is checking if one is checked... Any ideas would be greatly appreciated!

    Code:
    if(!(from.checkbox1.checked || form.checkbox2.checked || form.checkbox3.checked))
        {
          theMessage = theMessage + "\n --> Please select a checkbox";
        }

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    Code:
    if(!(form.checkbox1.checked  && form.checkbox3.checked) && !form.checkbox2.checked)
        {
          theMessage = theMessage + "\n --> Please select a checkbox";
        }
    Note your typo from.checkbox1.checked

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hey that worked perfectly! Is it possible to change it slightly so that checkbox 3 can be selected, and the form posted? checkbox 1 is the only checkbox that needs to have 3 checked also but 3 can be checked by itself if that makes sense?

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    Code:
    if(!(form.checkbox1.checked  && form.checkbox3.checked) && !form.checkbox3.checked && !form.checkbox2.checked)
        {
          theMessage = theMessage + "\n --> Please select a checkbox";
        }

  • Users who have thanked Fang for this post:

    tomyknoker (09-04-2007)

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi Fang... Thanks again... When I ran it again I realised I had to change a few things, but not even sure if it's possible... Ok for the checkboxes I changed it to this...

    Code:
    if(!(form.checkbox2.checked && form.checkbox3.checked) && !form.checkbox1.checked)
        {
          theMessage = theMessage + "\n --> Please choose the appropriate checkbox!";
        }
    So now it says that checkbox 2 & 3 need to be selected together or checkbox 1 can be selected by itself... Further down the form I have a gender select radio button, but I realised this only needs to be selected if checkbox 2 & 3 are selected, is there anyway to combine this?

    Code:
    myGender = -1;
    for (i=form.radio2.length-1; i > -1; i--) {
    if (form.radio2[i].checked) {
    myGender = i; i = -1;
    }
    }
    if (myGender == -1) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you must select your gender";
    }

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    Code:
    if((form.checkbox2.checked && form.checkbox3.checked) && !form.radio2[0].checked && !form.radio2[1].checked) {
    theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you must select your gender";
    }
    Consider hiding gender radios until checkbox 2 & 3 have been selected.
    It's a little more work, but clearer for the user what needs to be selected.


  •  

    Posting Permissions

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