...

View Full Version : Case or IF ?



WoG
08-31-2004, 06:58 PM
I'm trying to validate a form select box, and am validating based on the values of that select box after it's selected.

What I need to make sure is:
Value 1 of the gender field AND value 3 of the sport field are selected- If the ARE, then return true.

Do I need to pass this function 2 selectObj's in the declaration, since I am looking at 2 different form objects inside the function? Is my syntax correct for the AND in the IF statement? Should I use a Select CASE or the IF?

I'm not getting this to work. Any help would be appreciated.

Here is my fuction:
function SportGenderCheck(selectObj)
{
Select Case
if(document.athletic_prospect.gender.selectedIndex == "2" && document.athletic_prospect.sport.selectedIndex == "1") return true;

else if (document.athletic_prospect.gender.selectedIndex == "2" && document.athletic_prospect.sport.selectedIndex == "3") return true;

else if (document.athletic_prospect.gender.selectedIndex == "2" && document.athletic_prospect.sport.selectedIndex == "4") return true;

else if (document.athletic_prospect.gender.selectedIndex == "2" && document.athletic_prospect.sport.selectedIndex == "6") return true;

else if (document.athletic_prospect.gender.selectedIndex == "1" && document.athletic_prospect.sport.selectedIndex == "12") return true;

else if (document.athletic_prospect.gender.selectedIndex == "1" && document.athletic_prospect.sport.selectedIndex == "8") return true;

else if (document.athletic_prospect.gender.selectedIndex == "1" && document.athletic_prospect.sport.selectedIndex == "9") return true;
}

and here is the call:
if (SportGenderCheck(formObj.gender))
alert("The sport you selected does not match the correct gender");

AaronW
08-31-2004, 07:57 PM
It's not "select case" in JS. That's visual basic and possibly other languages, but I'm fairly sure Java doesn't use that... I do know it use switch (variable) { case: break; } though.

Try this:


function SportGenderCheck(selectObj)
{
gender = document.athletic_prospect.gender.selectedIndex;
sport = document.athletic_prospect.sport.selectedIndex;

switch (gender)
{
case 2:
return sport == 1 || sport == 3 || sport == 4 || sport == 6;
break;
case 1:
return sport == 12 || sport == 8 || sport == 9;
break;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum