Loading ...
Sorry, an error occurred while loading the content.

Re: Javascript Validation on Multiple Select List - Requiring User Input

Expand Messages
  • robertuva
    The problem is that I already have an onsubmit so how can I put this in the scripting up top that will prevent a person? I figure that I would incorporate it
    Message 1 of 3 , Jul 16, 2002
    • 0 Attachment
      The problem is that I already have an onsubmit so how can I put this
      in the scripting up top that will prevent a person? I figure that I
      would incorporate it into this portion of the script:

      <SCRIPT>
      maxSelect = 5;
      function take5(selObj){
      var j = 0;
      for(i=0;i<selObj.length ;i++ ){
      if(selObj.options[i].selected == true)
      {
      j++;
      if (j>maxSelect){
      alert ("Only 5 cities
      can be selected.");
      selObj.options
      [i].selected = false;
      }

      }
      }
      }
      </SCRIPT>


      --- In javascript3@y..., davidblackledge <no_reply@y...> wrote:
      > At the end of your take5 function (just before the last "}") add
      the
      > following lines:
      > if (j < 1) {
      > alert("At least one city must be selected.");
      > }
      >
      > This won't actually prevent them from submitting the form, though,
      > just alert them. A way of preventing submission is to add an
      > onsubmit event handliner to your form tag:
      > ...FORM onsubmit="var j=0;for(var
      > i = 0 ; selName.length > i ; ++i){
      > if(selName.options[i].selected) j++; }if(j==0){alert('At least one
      > city must be selected.');return false;} else return true;" ...
      >
      > David.
      >
      > --- In javascript3@y..., "robertuva" <robertuva@y...> wrote:
      > > I have built a form that can be found at the following URL:
      > >
      > > http://www.usairways.com/promotions/esavers/enroll/index.htm
      > >
      > > I am trying to find a javascript that requires the user to end a
      > city
      > > in the multiple select list. I have a javascript on there
      currently
      > > that limits the user input to five cities, but I can't seem to
      find
      > a
      > > javascript that will actually require that the user put at least
      one
      > > city. Can anyone help me?
      > >
      > > Below is the current javascript I have on the site. The variable
      > > name for the cities is selName:
      >
      > > <SCRIPT>
      > > maxSelect = 5;
      > > function take5(selObj){
      > > var j = 0;
      > > for(i=0;i<selObj.length ;i++ ){
      > > if(selObj.options[i].selected == true)
      > > {
      > > j++;
      > > if (j>maxSelect){
      > > alert ("Only 5 cities
      > > can be selected.");
      > > selObj.options
      > > [i].selected = false;
      > > }
      > >
      > > }
      > > }
      > > }
      > > </SCRIPT>
    Your message has been successfully submitted and would be delivered to recipients shortly.