Jump to content

time validation in javascript...


nezbo

Recommended Posts

Hi all i have this bit fo code an i want to to valicate the time and add it to the Hours section

 

what i am trying to do is get the start and end time, and date (month, day, and year). and calculate the hours between them using java script and then using inerHTML to update a field.

 

but the preblem that i have is i dont want weeneds to be included.

 

please any advise welcome.

 

here is the form/table :

 

<table width="100%" border="0">
  <tr valign="top">
    <td valign="top">Start Time/Date :</td>
    <td><input name="isTime" id="ipsTime" type="text" size="4" maxlength="4" /> 
        :  
        <input name="isDay" id="ipsDay" type="text" size="2" maxlength="2" />/<input name="isMonth" id="ipsMonth" type="text" size="2" maxlength="2" />/<input  name="isYear" id="ipsYear" type="text" size="4" maxlength="4" /> 
      </td><td rowspan="2" valign="middle"><input type="button" onclick="checkTimes()" value="Check Dates"/></td>
  </tr>

  <tr valign="top">
    <td valign="top">End Time/Date :</td>
    <td> <input name="ieTime" id="ipeTime" type="text" size="4" maxlength="4" /> 
        :  
        <input name="isDay" id="ipsDay" type="text" size="2" maxlength="2" />/<input name="ieMonth" id="ipeMonth" type="text" size="2" maxlength="2" />/<input name="ieYear" id="ipeYear" type="text" size="4" maxlength="4" /> 
      </td>
  </tr>
</table>

 

and i have this function and i am not to sure how to get it to work?

 

function checkTimes()
{

box = document.example.elements[];
var h = document.getElementById(values+"2");
h.innerHTML = '<<';
}

 

Cheers

Link to comment
Share on other sites

I have mamaged to get to this stage using a mktime and date function,

 

i have managed to get it to work sort of, but it is not quite right, i want it to only show 7.5 hours for 1 day and not show weekends.

 

any help appreciated?

 

here is my function and form :

 

 

 

  <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
  <table border="0"  valign="top">
  <tr valign="top">
    <td colspan="2" align="center"><h3>Holiday Request Form</h3></td>
  </tr>
  <tr valign="top">
    <td width="208"><div align="right">Name : </div></td>
    <td width="144">
<?php 

	$UserName = @mysql_query("SELECT * FROM person WHERE CallID = '$_COOKIE[user]'"); 
	while($UserName2 = mysql_fetch_array($UserName))
	{	
		echo $UserName2['FullName'];
	}

?>	</td>
  </tr>
  <tr valign="top">
    <td><div align="right">Grade/Position : </div></td>
    <td><?php 

	$UserName = @mysql_query("SELECT * FROM person WHERE CallID = '$_COOKIE[user]'"); 
	while($UserName2 = mysql_fetch_array($UserName))
	{	
		$GradeName = @mysql_query("SELECT * FROM grade  WHERE gradeID = '$UserName2[grade]'"); 
		while($GradeName2 = mysql_fetch_array($GradeName))
		{
			echo $GradeName2['gradeText'];
		}
	}

?>	</td>
  </tr>
  <tr valign="top">
    <td><div align="right">Date Of Application : </div></td>
    <td>
<?php 
	$theNowTime = gmmktime(); 
	echo date("H:i:s - j/m/Y", $theNowTime);
?>	</td>
  </tr>
  <tr valign="top">
    <td><div align="right">Date(S)/Time(S) : </div></td>
    <td><table width="100%" border="0">
  <tr valign="top">
    <td valign="top">Start Time/Date :</td>
    <td><input name="isTime" id="ipsTime" type="text" size="4" maxlength="4" /> 
        :  
        <input name="isDay" id="ipsDay" type="text" size="2" maxlength="2" />/<input name="isMonth" id="ipsMonth" type="text" size="2" maxlength="2" />/<input  name="isYear" id="ipsYear" type="text" size="4" maxlength="4" />      </td><td rowspan="2" valign="middle"><input type="button" onclick="checkTimes()" value="Check Dates"/></td>
  </tr>

  <tr valign="top">
    <td valign="top">End Time/Date :</td>
    <td> <input name="ieTime" id="ipeTime" type="text" size="4" maxlength="4" /> 
        :  
        <input name="isDay" id="ipeDay" type="text" size="2" maxlength="2" />/<input name="ieMonth" id="ipeMonth" type="text" size="2" maxlength="2" />/<input name="ieYear" id="ipeYear" type="text" size="4" maxlength="4" />      </td>
  </tr>
</table>      </td>
  </tr>
  <tr valign="top">
    <td><div align="right">Location (maybe more than one) : </div></td>
    <td> </td>
  </tr>
  <tr>
    <td><div align="right">
      <p>Total Holiday Request : </p>
      </div></td>
    <td><input type="text" name="theHours" id="theIHours" /></td>
  </tr>
</table>
</form>

 

function checkTimes()
{
<!-- Start Time -->
var st = document.getElementById("ipsTime").value;
var sd = document.getElementById("ipsDay").value;
var sm = document.getElementById("ipsMonth").value;
var sy = document.getElementById("ipsYear").value;
var sh = st.substring(0, 2);
var sMim = st.substring(3, 4);
var ss = "00";
<!-- /Start Time -->

<!-- End Time -->
var et = document.getElementById("ipeTime").value;
var ed = document.getElementById("ipeDay").value;
var em = document.getElementById("ipeMonth").value;
var ey = document.getElementById("ipeYear").value;
var eh = et.substring(0, 2);
var eMin = et.substring(3, 4);
var es = "00";
<!-- /End Time -->

var startMKTime = mktime(sh, sMim, ss, sm, sd,sy);
var endMKTime = mktime(eh, eMin, es, em, ed,ey);

if (startMKTime > endMKTime)
{
	alert("Start Time is greater than the end time");
}
else
{
	var addDay = (60 * 60);
	var newStartTime = startMKTime;

	var theHours = 0;

	var thesMonths = date('m', newStartTime);
	var theeMonths = date('m', endMKTime);

	var thesdays = date('d', newStartTime);
	var theedays = date('d', endMKTime);

	for (newStartTime > endMKTime; newStartTime < endMKTime; newStartTime + addDay)
	{	
		var dayes = date('l', newStartTime);
		if ((dayes == "Saturday") || (dayes == "Sunday"))
		{
			theHours = theHours
		}
		else
		{
			theHours = theHours + 7.5;
		}

		theHours = theHours + (endMKTime - startMKTime) / 60 / 60;
		document.getElementById("theIHours").value = theHours;
	}
}
}

Link to comment
Share on other sites

i have been working on this all day and i think i am geting close and i am now geting a proble that says "Stoping Scrips" "the script on this page is running slowley and may cause IE to become unresponsive"

 

can anyone see an error in my script that might be causing this?

 

 

function checkTimes()
{
<!-- Start Time -->
var st = document.getElementById("ipsTime").value;
var sd = document.getElementById("ipsDay").value;
var sm = document.getElementById("ipsMonth").value;
var sy = document.getElementById("ipsYear").value;
var sh = st.substring(0, 2);
var sMim = st.substring(3, 4);
var ss = "00";
<!-- /Start Time -->

<!-- End Time -->
var et = document.getElementById("ipeTime").value;
var ed = document.getElementById("ipeDay").value;
var em = document.getElementById("ipeMonth").value;
var ey = document.getElementById("ipeYear").value;
var eh = et.substring(0, 2);
var eMin = et.substring(3, 4);
var es = "00";
<!-- /End Time -->

var startMKTime = mktime(sh, sMim, ss, sm, sd,sy);
var endMKTime = mktime(eh, eMin, es, em, ed,ey);


if (startMKTime > endMKTime)
{
	alert("Start Time is greater than the end time");
}
else
{
	var addDay = (60 * 60);
	var newStartTime = startMKTime;

	var theHours = 0;

	var thesMonths = date('m', newStartTime);
	var theeMonths = date('m', endMKTime);

	var thesdays = date('d', newStartTime);
	var theedays = date('d', endMKTime);

	for (newStartTime = startMKTime; newStartTime <= endMKTime; newStartTime + addDay)
	{	
		//var dayes = date('l', newStartTime);
		//if ((dayes == "Saturday") || (dayes == "Sunday"))
		//{
		//	theHours = theHours
		//}
		//else
		//{
			var greaterThanADay = endMKTime - newStartTime;
			if (greaterThanADay > (24 * 60 * 60))
			{
				theHours = theHours + (7.5);
			}
			else 
			{
				theHours = theHours + (greaterThanADay / 60 / 60); 
			}
		//}

	}
	document.getElementById("theIHours").value = theHours;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.