Date_2 always higher Date_1 in the selectIndex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike1961
    New Member
    • Apr 2008
    • 66

    Date_2 always higher Date_1 in the selectIndex

    Hello everyone.

    I have this html page:

    Code:
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <select size="1" name="date_1" onChange="window.document.location='Tot.asp?date_2=24/04/2008&date_1='+this.options[this.selectedIndex].value;">
    <option>select</option>
    <option value="11/03/2008">11/03/2008</option>
    <option value="12/03/2008">12/03/2008</option>
    <option value="13/03/2008">13/03/2008</option>
    <option value="14/03/2008">14/03/2008</option>
    <option value="17/03/2008">17/03/2008</option>
    
    </select>
             
    <select size="1" name="date_2" onChange="window.document.location='Tot.asp?date_1=24/04/2008&date_2='+this.options[this.selectedIndex].value;">
    <option>select</option> 
    <option value="11/03/2008">11/03/2008</option>
    <option value="12/03/2008">12/03/2008</option>
    <option value="13/03/2008">13/03/2008</option>
    <option value="14/03/2008">14/03/2008</option>
    <option value="17/03/2008">17/03/2008</option>
    </select>
    
    
    </body>
    
    </html>
    I would like javascript function check that:

    date_1 = 24/04/2008
    date_2 = 25/04/2008

    it's right....

    date_1 = 25/04/2008
    date_2 = 24/04/2008

    it's wrong....

    Check that date_2 always higher date_1, it's possible?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You're setting document.locati on. This is deprecated in favour of window.location .href.

    Do you want to make the check onchange? You'll have to call a function to make the check before you change the URL unless you're planning to make the check on the server-side.

    To make sure Date_2 is always higher than Date1 , make two Date objects, set the dates using the setFullYear method and then make a simple comparison:
    [code=javascript]if (date1 > date2) //alert...[/code]
    Date Object reference

    Comment

    • Mike1961
      New Member
      • Apr 2008
      • 66

      #3
      Hi acoder.

      thanks for your reply, but i dont understand your suggestion :

      [php]

      <html>

      <head>


      <SCRIPT LANGUAGE=Javasc ript>

      var date_1, date_2;

      if ( date_1 > date_2 )
      alert( 'Stop !.' );


      </SCRIPT>

      </head>

      <body>

      <select size="1" name="date_1" onChange="windo w.location.href ='Tot.asp?date_ 2=24/04/2008&date_1='+t his.options[this.selectedIn dex].value;">
      <option>selec t</option>
      <option value="11/03/2008">11/03/2008</option>
      <option value="12/03/2008">12/03/2008</option>
      <option value="13/03/2008">13/03/2008</option>
      <option value="14/03/2008">14/03/2008</option>
      <option value="17/03/2008">17/03/2008</option>

      </select>

      <select size="1" name="date_2" onChange="windo w.location.href ='Tot.asp?date_ 1=24/04/2008&date_2='+t his.options[this.selectedIn dex].value;">
      <option>selec t</option>
      <option value="11/03/2008">11/03/2008</option>
      <option value="12/03/2008">12/03/2008</option>
      <option value="13/03/2008">13/03/2008</option>
      <option value="14/03/2008">14/03/2008</option>
      <option value="17/03/2008">17/03/2008</option>
      </select>


      </body>

      </html>

      [/php]

      Not working... can you tell me where the point when I was wrong ?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No, it doesn't work like that.

        Put the code into a function and call it, e.g.
        [code=javascript]function dateValidate() {
        // code goes here
        }[/code]

        See how to use the Date object here.

        When do you want to call this code? When you press the submit button?

        Comment

        • Mike1961
          New Member
          • Apr 2008
          • 66

          #5
          Originally posted by acoder
          No, it doesn't work like that.

          Put the code into a function and call it, e.g.
          [code=javascript]function dateValidate() {
          // code goes here
          }[/code]

          See how to use the Date object here.

          When do you want to call this code? When you press the submit button?
          OK, but this page is not a form...nothing submit button, only onchange event...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Mike1961
            OK, but this page is not a form...nothing submit button, only onchange event...
            So call the function in the onchange event handler. It might be a good idea to move the location.href into the function too. If it validates, send the user off to the next page. If it doesn't, alert the error.

            Comment

            • Mike1961
              New Member
              • Apr 2008
              • 66

              #7
              One example, please acoder... I am newbie...

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                OK, have a look at this example:
                [code=javascript]<script type="text/javascript">
                function validateDate() {
                var date1, date2, date_1, date_2;
                // get date1 value
                date1 = document.getEle mentById("date1 ").value;
                // split the values to get the date/month/year
                date1 = date1.split("/");
                //create new date object
                date_1 = new Date();
                // set the date - see ref
                date_1.setFullY ear(date1[2],date1[1]-1,date1[0]);
                // ...
                // repeat for date2...
                //...
                // now compare
                if ( date_1 > date_2 ) {
                alert( 'Stop !.' );
                else {
                window.location .href = 'Tot.asp?date_1 =' + encodeURICompon ent(date1.join( "/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));
                }
                }
                [/code]For the select elements, add ids and change the onchange to call the function:[code=html]<select id="date1" size="1" name="date_1" onchange="valid ateDate()">[/code]

                Comment

                • Mike1961
                  New Member
                  • Apr 2008
                  • 66

                  #9
                  Thanks x your reply, but not working... the link:

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You'll also have to add a check that the date1 and date2 values are not empty (the first option Seleziona) :
                    [code=javascript]if (date1.value == "") return;[/code]Add a value attribute for the first option:
                    [code=html]<select ...>
                    <option value="">Selezi ona</option>[/code]

                    Comment

                    • Mike1961
                      New Member
                      • Apr 2008
                      • 66

                      #11
                      Originally posted by acoder
                      You'll also have to add a check that the date1 and date2 values are not empty (the first option Seleziona) :
                      [code=javascript]if (date1.value == "") return;[/code]Add a value attribute for the first option:
                      [code=html]<select ...>
                      <option value="">Selezi ona</option>[/code]
                      Sorry... I can not... :

                      [php]
                      code:<HTML>
                      <HEAD>
                      <TITLE>2007</TITLE>

                      <script type="text/javascript">

                      function validateDate()

                      {
                      var date1, date2, date_1, date_2;
                      // get date1 value
                      date1 = document.getEle mentById("date1 ").value;
                      // split the values to get the date/month/year
                      date1 = date1.split("/");
                      //create new date object
                      date_1 = new Date();
                      // set the date - see ref
                      date_1.setFullY ear(date1[2],date1[1]-1,date1[0]);

                      // repeat for date2...
                      // get date2 value
                      date2 = document.getEle mentById("date2 ").value;
                      // split the values to get the date/month/year
                      date2 = date2.split("/");
                      //create new date object
                      date_2 = new Date();
                      // set the date - see ref
                      date_2.setFullY ear(date2[2],date2[1]-1,date2[0]);

                      //...
                      // now compare

                      if (date1.value == "") return;

                      if ( date_1 > date_2 )

                      {
                      alert ("Stop");
                      }

                      else

                      {
                      window.location .href = 'index.htm?date _1=' + encodeURICompon ent(date1.join( "/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));


                      }
                      }

                      </script>
                      </HEAD>
                      <BODY>

                      <select id="date1" name="date_1" onchange="valid ateDate()">
                      <option value="">Selezi ona</option>
                      <option value="19/07/2007">19/08/2007</option>
                      <option value="19/08/2007">19/09/2007</option>
                      <option value="19/09/2007">19/10/2007</option>
                      </select>

                      <select id="date2" name="date_2" onchange="valid ateDate()">
                      <option value="">Selezi ona</option>
                      <option value="29/07/2007">29/05/2007</option>
                      <option value="29/08/2007">29/06/2007</option>
                      <option value="29/09/2007">29/07/2007</option>
                      </select>

                      </BODY>
                      </HTML>

                      [/php]

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        You didn't add the JavaScript:[code=javascript] // get date1 value
                        date1 = document.getEle mentById("date1 ").value;
                        if (date1 == "") return;
                        [/code]

                        Comment

                        • Mike1961
                          New Member
                          • Apr 2008
                          • 66

                          #13
                          Originally posted by acoder
                          You didn't add the JavaScript:[code=javascript] // get date1 value
                          date1 = document.getEle mentById("date1 ").value;
                          if (date1 == "") return;
                          [/code]
                          thanks, but:

                          Object doesn't support this property or method
                          in the line:

                          [php]
                          window.location .href = 'index.htm?date _1=' + encodeURICompon ent date1.join("/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));
                          [/php]

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            The opening bracket went missing:[code=javascript]window.location .href = 'index.htm?date _1=' + encodeURICompon ent(date1.join( "/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));
                            [/code]

                            Comment

                            • Mike1961
                              New Member
                              • Apr 2008
                              • 66

                              #15
                              No... the same error...

                              [php]
                              code:<HTML>
                              <HEAD>
                              <TITLE>2007</TITLE>

                              <script type="text/javascript">

                              function validateDate()

                              {
                              var date1, date2, date_1, date_2;
                              // get date1 value
                              date1 = document.getEle mentById("date1 ").value;
                              // split the values to get the date/month/year
                              date1 = date1.split("/");
                              //create new date object
                              date_1 = new Date();
                              // set the date - see ref
                              date_1.setFullY ear(date1[2],date1[1]-1,date1[0]);

                              // repeat for date2...
                              // get date2 value
                              date2 = document.getEle mentById("date2 ").value;
                              // split the values to get the date/month/year
                              date2 = date2.split("/");
                              //create new date object
                              date_2 = new Date();
                              // set the date - see ref
                              date_2.setFullY ear(date2[2],date2[1]-1,date2[0]);

                              //...
                              // now compare

                              date1 = document.getEle mentById("date1 ").value;
                              if (date1.value == "") return;

                              if ( date_1 > date_2 )

                              {
                              alert ("Stop");
                              }

                              else

                              {
                              //window.location .href = 'index.htm?date _1=' + encodeURICompon ent(date1.join( "/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));
                              window.location .href = 'index.htm?date _1=' + encodeURICompon ent(date1.join( "/")) + '&date_2=' + encodeURICompon ent(date2.join( "/"));

                              }
                              }

                              </script>
                              </HEAD>
                              <BODY>

                              <select id="date1" name="date_1" onchange="valid ateDate()">
                              <option value="">Selezi ona</option>
                              <option value="19/07/2007">19/08/2007</option>
                              <option value="19/08/2007">19/09/2007</option>
                              <option value="19/09/2007">19/10/2007</option>
                              </select>

                              <select id="date2" name="date_2" onchange="valid ateDate()">
                              <option value="">Selezi ona</option>
                              <option value="29/07/2007">29/05/2007</option>
                              <option value="29/08/2007">29/06/2007</option>
                              <option value="29/09/2007">29/07/2007</option>
                              </select>

                              </BODY>
                              </HTML>

                              [/php]

                              Comment

                              Working...