SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Compare time in javascript

    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 inserted in the forn is before or less of the time of PC ( or with a specific reference time ) to stop the compilation this form ?

    This is my page web... not work.... :

    PHP Code:

    <html>

    <
    head>

    <
    script>
    function 
    validTime(T){
        
    TT.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;
    }

    </script>
    </head>

    <body>

    <form method="POST" action="other.htm"  onsubmit="return(validTime(this));">
        <p><input type="text" name="T1" size="20"></p>
        <p><input type="submit" value="GO" name="B1"></p>
    </form>

    </body>

    </html> 
    Thanks for your attention.
    Mike

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Your function accepts a string which is supposed to be a representation of the time.
    Currently the function is failing because the T value is instead the form itself, which is passed to it as the this keyword from the onsubmit attribute.

    You will need to instead either get the form value from the function, or pass the time value into the function for things to work better.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thanks x your reply.

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


    PHP Code:
    <html>
     
    <
    head>
     
    <
    script language="javascript" type="text/javascript">
     
    <!--
    function 
    ValidateForm(Qform
    {
     
      for (var 
    0Qform.elements.lengtha++) 
        {
        var 
    campo Qform.elements[a];
     
            if (
    campo.value.length <= 0)
     
            {
                
    window.alert('KO-1');
                
    campo.focus();
                return 
    false;
              }
              
         }
              
    var 
    re = new RegExp("^([0-9]{2}):([0-9]{2})$""");
     
    if (!
    Qform.T1.value.match(re))
        {
          
    alert("KO-2");
          
    Qform.T1.focus();
          
    Qform.T1.select();
          return(
    false);
        }   
     
     
        
    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()) 
        
        
        {
          
    alert("KO-3");
          
    Qform.T1.focus();
          
    Qform.T1.select();
          return(
    false);      
        }  
     
                 
    return(
    true);
    }
     
    //-->
    </script>
    </head>
     
    <body>
     
    <form action="other.htm" method="post" name="Qform" onsubmit="return(ValidateForm(this));">
        <p><input type="text" name="T1" size="20"></p>
        <p><input type="submit" value="GO" name="B1"></p>
    </form>
     
    </body>
     
    </html> 

  4. #4
    SitePoint Evangelist Miguel61's Avatar
    Join Date
    Mar 2008
    Posts
    408
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Any idea ?

  5. #5
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    el.value.split(/\D+/);
    Code:
     
    <script type="text/javascript">
    
    Date.prototype.Miguel61=function(){
    var d2=new Date();
    var h =  60;
    var s1 = (d2.getHours() * h) + d2.getMinutes()  ;
    var s2 = (this.getHours() * h ) + this.getMinutes()  ;
    var f= Math.abs(s1 - s2) ;
    return  Math.floor(f / h) + " hours : " + (f%h) + " minutes" ; 
    }
    
    function kontrol(){
    var el=document.getElementById('inp1');
    var s=el.value.split(/\D+/);  // or  var s=el.value.split(':');
    var d1=new Date();
    d1.setHours(s[0]);
    d1.setMinutes(s[1]);
    alert(d1.Miguel61());
    }
    
    </script>
    </head>
    <body>
    <form  onsubmit="return kontrol()">
    <input type="text" id="inp1"><br>
    <input type="submit" value="gönder">
    </form>
    Last edited by muazzez; Jun 6, 2008 at 09:50.
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.


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
  •