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 2 of 2

Thread: Case or IF ?

  1. #1
    WoG
    WoG is offline
    New to the CF scene
    Join Date
    May 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Case or IF ?

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

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    1,223
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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;
      }
    }


  •  

    Posting Permissions

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