Compare time in the format hh: mm

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

    #1

    Compare time in the format hh: mm

    Hi everyone.

    Whit JavaScript it's posible compare time inserted into a form ( in the format hh: mm ), with the time of your PC ?

    If this time is inserted before or less of the time of PC ( or with a specific reference time ) to stop the compilation this form ?

    Thanks for your attention.
    Mike
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    There is no Time object native to javascript, but you can use the Date object, which has methods to set and get hours and minutes (and seconds and milliseconds).

    But you can't create a date with only hours and minutes- you need the year, month and day as well. You could assign the month and year to be whatever year and month is on the client computer, but you may want them to include it.

    If you provide a valid string that can be parsed to a date object you can compare it to the current Date() read from the client. It the time input is less than the current time, return false to an onsubmit call to prevent the form submission.

    Otherwise return true.
    Code:
    function validTime(T){
    	T= T.split(/\D+/);
    	var D1= new Date();
    	D1= new Date(D1.setHours(T[0]*1,T[1]*1,0,0));
    	if(!D1 || D1<new Date()) return false;
    	return true;
    }
    //test
    alert(validTime ('10:30'))

    Comment

    • Mike1961
      New Member
      • Apr 2008
      • 66

      #3
      Thanks for your reply; but I not understand...

      This is my page web:

      [php]

      <html>

      <head>

      <script>
      function validTime(T){
      T= T.split(/\D+/);
      var D1= new Date();
      D1= new Date(D1.setHour s(T[0]*1,T[1]*1,0,0));
      if(!D1 || D1<new Date()) return false;
      return true;
      }

      </script>
      </head>

      <body>

      <form method="POST" action="other.h tm" onsubmit="retur n(validTime(thi s));">
      <p><input type="text" name="T1" size="20"></p>
      <p><input type="submit" value="GO" name="B1"></p>
      </form>

      </body>

      </html>

      [/php]

      Not work....

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        You are not passing the correct argument to the function.

        <form method= "POST" action="other.h tm"
        onsubmit="return validTime(this. T1.value) ;">

        Comment

        • Mike1961
          New Member
          • Apr 2008
          • 66

          #5
          Thanks... I understand.... but it's possible have everything in the same function javascript?

          Function ValidateForm + function validTime ???

          This is the complete code:

          [php]

          <html>

          <head>


          <script language="javas cript" type="text/javascript">

          <!--
          function ValidateForm(Qf orm)
          {

          for (var a = 0; a < Qform.elements. length; a++)
          {
          var campo = Qform.elements[a];

          if (campo.value.le ngth <= 0)

          {
          window.alert('K O.');
          campo.focus();
          return false;
          }

          }

          var re = new RegExp("^([0-9]{2}):([0-9]{2})$", "");

          if (!Qform.T1.valu e.match(re))
          {
          alert("KO");
          Qform.T1.focus( );
          Qform.T1.select ();
          return(false);
          }


          //Abilita l'invio del form
          return(true);
          }

          //-->
          </script>

          </head>

          <body>


          <!-- Form -->

          <form action="other.h tm" method="post" name="Qform" onsubmit="retur n(ValidateForm( this));">

          <p><input type="text" name="T1" size="20"></p>
          <p><input type="submit" value="GO" name="B1"></p>
          </form>

          </body>

          </html>

          [/php]

          Comment

          • Mike1961
            New Member
            • Apr 2008
            • 66

            #6
            I write this but not working... 'T' is undefined....

            [php]

            <html>

            <head>

            <script language="javas cript" type="text/javascript">

            <!--
            function ValidateForm(Qf orm)
            {

            for (var a = 0; a < Qform.elements. length; a++)
            {
            var campo = Qform.elements[a];

            if (campo.value.le ngth <= 0)

            {
            window.alert('K O-1');
            campo.focus();
            return false;
            }

            }

            var re = new RegExp("^([0-9]{2}):([0-9]{2})$", "");

            if (!Qform.T1.valu e.match(re))
            {
            alert("KO-2");
            Qform.T1.focus( );
            Qform.T1.select ();
            return(false);
            }


            T = T.split(/\D+/);
            var D1 = new Date();

            D1 = new Date(D1.setHour s(T[0]*1,T[1]*1,0,0));

            if(!D1 || D1 < new Date())


            {
            alert("KO-3");
            Qform.T1.focus( );
            Qform.T1.select ();
            return(false);
            }


            return(true);
            }

            //-->
            </script>
            </head>

            <body>

            <form action="other.h tm" method="post" name="Qform" onsubmit="retur n(ValidateForm( this));">
            <p><input type="text" name="T1" size="20"></p>
            <p><input type="submit" value="GO" name="B1"></p>
            </form>

            </body>

            </html>


            [/php]

            Comment

            • Mike1961
              New Member
              • Apr 2008
              • 66

              #7
              Any idea ?
              Mike.....

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Before line 36 (e.g. line 35), add:
                [code=javascript]var T = Qform.T1.value;[/code]T is undefined because you haven't defined it anywhere. It was previously passed as an argument to the function.

                Comment

                • Mike1961
                  New Member
                  • Apr 2008
                  • 66

                  #9
                  Thanks acoder for your suggestion.... now it's working !!!!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    A tip: look at how you're using variables and passing arguments to functions. In the first instance, you had a problem with passing the form instead of the text box value. When you combined the functions, you were using the same variable name, but you were now passing the form to the function and T hadn't been defined.

                    Anyway, glad it's solved. If you have more questions, you know where to come.

                    Comment

                    Working...