SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I am trying to check the user enter time with current system time

    Hi

    All

    I am trying to check the user enter time with current system.

    If user enter the time less than the current system time then I want to display the alert box. But the code is not working.


    Here is the code

    <html>
    <head>
    <title>test</title>

    <script type="text/javascript">
    var d = new Date();

    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();

    var test = curr_hour + " : " + curr_min;
    var test2=document.f1.t1.value;
    document.write(test);
    function check()
    {
    if (test<test2)
    {

    alert("please enter the time greater than current time");
    }
    }
    </script>
    </head>
    <body>
    <form name="f1">
    <input type='text' name='t1' id='t1' value=''onblur='check()';>
    </form>
    </body>
    </html>

    please help me out


    Thanks
    M.Samiuddin

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this, you had a couple of errors in your JS and HTML.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript">
    function check() {
        var d = new Date();
        var hour = d.getHours(), mins = d.getMinutes();
        
        var current = hour + ':' + mins, user = document.f1.t1.value;
        
        if (user < current) {
            alert('Please enter the time greater than current time');
        }
    }
    </script>
    </head>
    <body>
    
    <form name="f1">
        <input type="text" name="t1" id="t1" value="" onblur="check();" />
    </form>
    
    </body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you response

    The code is working fine, but when I am entering time 8:00 the alert box is not coming
    but when I am entering time 11:00 the alert box is displaying

    please help me out

    Thanks
    MD.Samiuddin

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    If possible please provide the code that should give user the flexibility to pickup time
    like time picker

  5. #5
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I tested the code i posted and it worked fine, if the current time was for example 18:56 and i entered 18:55 it would show the error but anything above it would work.

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah that's true but when I am giving like 9:00 and system time is 11:00 the alert box is not working


    Thanks

  7. #7
    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)
    Quote Originally Posted by samiuddin.adse View Post
    yeah that's true but when I am giving like 9:00 and system time is 11:00 the alert box is not working
    That's due to the user and current variables both being strings. Strings are not appropriate when performing date/time comparisons.

    Use the Date object for both variables, and your comparisons will work as expected.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    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)
    For example:

    Code javascript:
    function check() {
        var now = new Date(),
            timeParts = document.f1.t1.value.split(':'),
            userTime = new Date();
        userTime.setHours(timeParts[0]);
        userTime.setMinutes(timeParts[1]);
        if (userTime < now) {
            alert('Please enter the time greater than current time');
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your response.
    I need to convert the value which I am getting from the text box to time, and I want to give input mask to time field like 11:00:00 when user enters hours the cursor should move to minutes and then cursor should move to seconds.

    Regards
    MD.Samiuddin

  10. #10
    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)
    Quote Originally Posted by samiuddin.adse View Post
    thanks for your response.
    I need to convert the value which I am getting from the text box to time, and I want to give input mask to time field like 11:00:00 when user enters hours the cursor should move to minutes and then cursor should move to seconds.
    That can be done, but it takes a lot of code to do it right.

    How about a pre-built solution, such as a timepicker?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    Paul, for you help, the time picker I have to download the Jquery.


    Thanks once again
    MD.Samiuddin

  12. #12
    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)
    Quote Originally Posted by samiuddin.adse View Post
    Paul, for you help, the time picker I have to download the Jquery.
    There may-well be others out there that don't require jQuery. I just hope that they're as easily to get going.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •