SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with check date and hours

    Help with check date and hours

    Hi all.

    I have this form:

    Code:
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <form name="orario" method="post" action="">
    
    <input type="text" name="hour" size="20">
    
    <input type="text" name="dates" size="20">
    
    <input type="submit" value="Invia" name="B1">
    
    </form>
    </body>
    
    </html>
    I need one function javascript check date and time insert in the form.

    If date is same the date now and the hour is less two hours the hours now, the form is stopped.

    For example:

    date now = 22/09/2008
    hours now = 14:50

    date = 22/09/2008
    hours = 15:30 ===> KO

    date = 22/09/2008
    hours = 16:49 ===> KO

    date = 22/09/2008
    hours = 16:50 ===> OK

    Can you help me?
    kind regards
    viki

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need an onsubmit event handler on the FORM element that returns true if the condition for submitting the form is met, or false otherwise. Note that this still won't work if the user has JavaScript disabled.

    Code:
    <form ... onsubmit="return checkDate();">
    ...
    </form>

  3. #3
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    You need an onsubmit event handler on the FORM element that returns true if the condition for submitting the form is met, or false otherwise. Note that this still won't work if the user has JavaScript disabled.

    Code:
    <form ... onsubmit="return checkDate();">
    ...
    </form>
    sorry i dont understand...

    I check this:

    * Date register is same at today;
    * Hours register is less two hours the now
    Last edited by viki1967; Sep 22, 2008 at 08:06.

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by viki1967 View Post
    sorry i dont understand...

    I check this:

    * Date register is same at today;
    * Hours register is less two hours the now
    You would have to write a JavaScript function that does those things and use it in the form's onsubmit handler. I don't know how to explain it any simpler than that without giving you a crash course in JavaScript and the HTML DOM - which, of course, I have no intention of doing. I suggest finding some online tutorials that explain these things.

  5. #5
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    You would have to write a JavaScript function that does those things and use it in the form's onsubmit handler. I don't know how to explain it any simpler than that without giving you a crash course in JavaScript and the HTML DOM - which, of course, I have no intention of doing. I suggest finding some online tutorials that explain these things.
    OK I try this:

    Code:
    var stringaData = document.orario.Data_Visita.value.split("/");
    
    var miaData = new Date(stringaData[2],stringaData[1]-1,stringaData[0]);
    var oggi = new Date();
    
    if (miaData>oggi)
    
    {
    
          alert("error");
          document.orario.Data_Visita.focus();
    	  return false;
        	
        	}
    But not validate this, it's right:
    date = 23/09/2008
    hours = 15:30

    Validate this, it's wrong:
    date = 13/09/2008
    hours = 18:30

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code doesn't take into account the hours and minutes when comparing the old date/time to the current date/time. Also, it must be inside a function to return a value. Try this:

    Code:
    <form ... onsubmit="return checkDate();">
    ...
    </form>
    <script type="text/javascript">
    function checkDate() {
     var dateStr = document.orario.date.value.split("/");
     var timeStr = document.orario.time.value.split(":");
     var oldDate = new Date(dateStr[2],dateStr[1]-1,dateStr[0],timeStr[0],timeStr[1]);
     var currDate = new Date();
     // Convert date/time to integers for mathematical comparison.
     var oldTimestamp = oldDate.valueOf();
     var currTimestamp = currDate.valueOf();
     // Since the value is expressed in milliseconds, we want to
     // compare the elapsed time to the number of milliseconds
     //  in two hours.
     if ((currTimestamp - oldTimestamp) < (1000 * 60 * 60 * 2)) return false;
     else return true;
    }
    </script>
    This was thrown together in a hurry, but it's in the ballpark.

  7. #7
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but not working...

    validate this, is wrong:
    08/09/2008
    23:55

    not validate this, is wrong:
    22/09/2008
    23:55

    not validate this, is right:
    23/09/2008
    23:55

    Is valid only for today and hours is less two hours the now


  8. #8
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have made an error somewhere, or perhaps have not understood the question correctly. Perhaps someone else can spot the problem. Sorry.

  9. #9
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help and sorry for my english.

  10. #10
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by viki1967 View Post
    thanks for your help and sorry for my english.
    No worries. Perhaps changing < to > would fix it, although that doesn't seem logical. I'd prefer not to kludge the code for the sake of a hasty fix.

  11. #11
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the form is valid when:

    1) value of field 'date' is today ;
    2) value of field 'time' is not the same two hours back from the hours of client...

    Not is logic ?

  12. #12
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by viki1967 View Post
    the form is valid when:

    1) value of field 'date' is today ;
    2) value of field 'time' is not the same two hours back from the hours of client...

    Not is logic ?
    In other words, the form should submit if the difference is more than two hours? In that case, reverse the comparison operator (< should be >).


Bookmarks

Posting Permissions

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