Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should be simple solution to If statement

Status
Not open for further replies.

donniea21

MIS
Feb 16, 2001
75
0
0
US
Here is my code for an if statement

if(document.forms[0].HLTcov[3].checked)
{
if(cctEval == "false")
{alert("Please select a child dependent covered under Medical.");
return false;}
if(!document.forms[0].hltSpouseChk.checked)
{alert("Please select spouse covered under Medical.");
return false;}
}

else if(document.forms[0].HLTcov[1].checked)
{
if(!document.forms[0].hltSpouseChk.checked)
{alert("Please select spouse covered under Medical.");
return false;}
}


else if(document.forms[0].HLTcov[2].checked)
{
if(!document.forms[0].hltChildChk.checked)
{alert("Please select a child dependent covered under Medical.");
return false;}
}

The problem i am having is that if the first if "if(document.forms[0].HLTcov[3].checked)" is true, it goes down to the second else if "else if(document.forms[0].HLTcov[2].checked)" and skip the second one
 
sorry i am a bit confused. i think if the first if statement evaluates to true then none of the following else if statements should execute. if the top if evaluates to false then the following else if will be tested and if this is true then no following else if will be executed and so on....

could you explain again what you want?

regards

rob
 
I am checking radio buttons against checkboxes..ie i have a radio button so an employee can select family to be covered by medical insurance...i need to make sure that if family is selected that they check at least their spouse and one child..if they decide to change the radio from family to spouse then i need to make sure they check their spouse and so on...When the form loads, my example employee is defaulted to the family radio being selected "document.forms[0].HLTcov[3].checked)" so if they submit without checking a spouse and at least one child they get an alert..this works fine...but if they choose the spouse radio, the first "if" should be false and the the else if(document.forms[0].HLTcov[1].checked) should be true...it is not getting into that part of the function..i can submit without checking the spouse checkbox...I now this is confusing sounding so if its a hassle dont worry about it
 
I'm not sure exactly where to start here. I usually try to get the flow down on paper first if its going to be complex.

Its also a good idea in my opinion to avoid using NOT '!' where ever possible because it tends to complicates things for me anyway.

Sorry cant be much more help.
Nice one :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top