By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Compare time in the format hh: mm

P: 66
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
Jun 1 '08 #1
Share this Question
Share on Google+
9 Replies


100+
P: 428
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.
Expand|Select|Wrap|Line Numbers
  1. function validTime(T){
  2.     T= T.split(/\D+/);
  3.     var D1= new Date();
  4.     D1= new Date(D1.setHours(T[0]*1,T[1]*1,0,0));
  5.     if(!D1 || D1<new Date()) return false;
  6.     return true;
  7. }
  8.  
//test
alert(validTime('10:30'))
Jun 2 '08 #2

P: 66
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.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>

[/php]

Not work....
Jun 2 '08 #3

100+
P: 428
You are not passing the correct argument to the function.

<form method= "POST" action="other.htm"
onsubmit="return validTime(this.T1.value) ;">
Jun 2 '08 #4

P: 66
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="javascript" type="text/javascript">

<!--
function ValidateForm(Qform)
{

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

if (campo.value.length <= 0)

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

}

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

if (!Qform.T1.value.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.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>

[/php]
Jun 2 '08 #5

P: 66
I write this but not working... 'T' is undefined....

[php]

<html>

<head>

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

<!--
function ValidateForm(Qform)
{

for (var a = 0; a < Qform.elements.length; a++)
{
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 = 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>


[/php]
Jun 2 '08 #6

P: 66
Any idea ?
Mike.....
Jun 3 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Before line 36 (e.g. line 35), add:
Expand|Select|Wrap|Line Numbers
  1. var T = Qform.T1.value;
T is undefined because you haven't defined it anywhere. It was previously passed as an argument to the function.
Jun 3 '08 #8

P: 66
Thanks acoder for your suggestion.... now it's working !!!!
Jun 3 '08 #9

acoder
Expert Mod 15k+
P: 16,027
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.
Jun 4 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.