...

View Full Version : Making Sure Checkboxes Are Checked



tomyknoker
09-03-2007, 09:50 AM
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!


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

Fang
09-03-2007, 10:11 AM
if(!(form.checkbox1.checked && form.checkbox3.checked) && !form.checkbox2.checked)
{
theMessage = theMessage + "\n --> Please select a checkbox";
}

Note your typo from.checkbox1.checked

tomyknoker
09-03-2007, 10:52 AM
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?

Fang
09-03-2007, 11:29 AM
if(!(form.checkbox1.checked && form.checkbox3.checked) && !form.checkbox3.checked && !form.checkbox2.checked)
{
theMessage = theMessage + "\n --> Please select a checkbox";
}

tomyknoker
09-04-2007, 06:39 AM
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...


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?


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";
}

Fang
09-04-2007, 09:43 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum