Skip navigation
Currently Being Moderated

Simple javascript help?

Sep 11, 2012 11:53 PM

I've got 2 'select'  tags in my form allowing a user to choose tour_1 or tour_2 BUT NOT both. Tour_1 and Tour_2 can't be 'options' in the same select tag as they have different pick-up points which are listed in the seperate 'select' tags. I dont want the user to be able to send the form unless one or the other of the 'select' tags has been chosen BUT NOT both.

 

So I can t get one working:

 

var pp = document.forms.purchaseTickets.tour_1.value;

var pa = document.forms.purchaseTickets.tour_2.value;

 

if (pp == null || pp == "")

{

    alert("Please choose a pick-up point!");

    return false;

}

 

This only checks that tour_1 'select' tag is not empty BUT if I also select something from  tour_2 'select' tag it also will allow that to be sent as well, bum! bum!

 

I can't check for both 'select' tags being empty as that will only prompt the user to choose a selection from both,

 

So what I need is to allow the form to be sent if the user has chosen an option from one or the other of the 'select' tags but not if they have chosen and option from both by mistake.

 

Cheers

 

Os.

 
Replies
  • Currently Being Moderated
    Sep 12, 2012 5:37 AM   in reply to osgood_

    SELECT does not give a value, directly.  You can use selectedIndex, or the value of the option that is selected (again, selectedIndex).

     

    Assuming that the form name is purchaseTickets:

     

    var pp = document.forms["purchaseTickets"].tour_1;

    var pa = document.forms["purchaseTickets"].tour_2;

     

    if(pp.selectedIndex == 0 && pa.selectedIndex == 0) { // Neither option selected

      do stuff

    }

    if(pp.selectedIndex != 0 && pa.selectedIndex !=0) { // Both options selected

      so something else

    }

     

    ^_^

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 12, 2012 6:17 AM   in reply to osgood_

    Glad I could help.  Please mark this as resolved.  Let me know if you have any other JS questions.

     

    ^_^

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points