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

Try this, you had a couple of errors in your JS and HTML.

<!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>

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

Hi

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

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.

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

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.

For example:


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');
    }
}

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

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?

Thanks

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

Thanks once again
MD.Samiuddin

There may-well be others out there that don’t require jQuery. I just hope that they’re as easily to get going.