Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2007, 08:50 AM   PM User | #1
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
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";
    }
tomyknoker is offline   Reply With Quote
Old 09-03-2007, 09:11 AM   PM User | #2
Fang
Regular Coder

 
Join Date: Jun 2004
Posts: 495
Thanks: 0
Thanked 82 Times in 80 Posts
Fang is on a distinguished road
Code:
if(!(form.checkbox1.checked  && form.checkbox3.checked) && !form.checkbox2.checked)
    {
      theMessage = theMessage + "\n --> Please select a checkbox";
    }
Note your typo from.checkbox1.checked
Fang is offline   Reply With Quote
Old 09-03-2007, 09:52 AM   PM User | #3
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
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?
tomyknoker is offline   Reply With Quote
Old 09-03-2007, 10:29 AM   PM User | #4
Fang
Regular Coder

 
Join Date: Jun 2004
Posts: 495
Thanks: 0
Thanked 82 Times in 80 Posts
Fang is on a distinguished road
Code:
if(!(form.checkbox1.checked  && form.checkbox3.checked) && !form.checkbox3.checked && !form.checkbox2.checked)
    {
      theMessage = theMessage + "\n --> Please select a checkbox";
    }
Fang is offline   Reply With Quote
Users who have thanked Fang for this post:
tomyknoker (09-04-2007)
Old 09-04-2007, 05:39 AM   PM User | #5
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
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";
}
tomyknoker is offline   Reply With Quote
Old 09-04-2007, 08:43 AM   PM User | #6
Fang
Regular Coder

 
Join Date: Jun 2004
Posts: 495
Thanks: 0
Thanked 82 Times in 80 Posts
Fang is on a distinguished road
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.
Fang is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:34 PM.


Advertisement
Log in to turn off these ads.