473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Compare time in the format hh: mm

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
9 2879
mrhoo
428 256MB
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
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
mrhoo
428 256MB
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
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
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
Any idea ?
Mike.....
Jun 3 '08 #7
acoder
16,027 Expert Mod 8TB
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
Thanks acoder for your suggestion.... now it's working !!!!
Jun 3 '08 #9
acoder
16,027 Expert Mod 8TB
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

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

Similar topics

4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
4
by: JIM.H. | last post by:
Hello, I put a compare validator in to my applications and set the type as Date. It works for “MM/dd/yyyy” but not for “MM/dd/yyyy hh:mm:ss”. How can I validate time part too? Thanks,
1
by: Manny Chohan | last post by:
i have two date fields for event start date and end date. I let the user enter two on create event page. on submit, i need to compare if the end date is earlier than start date or not. How can i...
3
by: scorpion53061 | last post by:
Could you look at this function and tell me why I am getting an exception concerning date cast on line set apart by stars... If you have better suggestions of how to do this I would be open to...
3
by: Peter | last post by:
ASP.NET 1.1, this is a Intranet application and runs only on IE. I have two webform text boxes which contain dates - starting date and ending date. The dates could be in any format. for...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.