Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: total hours validation


Message #1 by "HoayFern" <hoayfern.lee@d...> on Fri, 2 Nov 2001 13:50:42 -0800
I have this code to validate total hours but i dunno whether is it working
or not

<SCRIPT LANGUAGE="Javascript">
<!--
/*
if the employee work on saturday and sunday from 8.30 to 6pm, we need to
deduct 45 minutes

if the employee work on saturday and sunday from 8.30am to 1pm, we dont need
to deduct 45 minutes.

*/

function CalcHours(theForm) {
  var today = new Date();
  var weekend = (today.getDay==6 || today.getDay==0)?true:false;
  var lunch = false;
  var t1Hour 
theForm.FromHour.options[theForm.FromHour.selectedIndex].value-0;
  var t1Min  
theForm.FromMin.options[theForm.FromMin.selectedIndex].value-0;
  var t2Hour = theForm.ToHour.options[theForm.ToHour.selectedIndex].value-0;
  var t2Min  = theForm.ToMin.options[theForm.ToMin.selectedIndex].value-0;
  var t1 = t1Hour*60+t1Min;
  var t2 = t2Hour*60+t2Min;
  if (t1>0 && t2>0) {
     var diff=t2-t1;
     if (weekend && diff>=570) {
        diff -=45
        lunch = true
     }
     if (diff<0 || isNaN(diff)) {
 alert('Wrong entry')
 theForm.Total.value ='Invalid';
 }
 else {
        hours = parseInt(diff/60)
        min = diff-(hours*60)
        theForm.Total.value = hours+':'+((min<10)?'0'+min:min) + ((lunch)?'
(-45)':'');
     }
  }
}
//-->
</SCRIPT>

at the end of the page, i have this line :

<P align=left><INPUT type="button" NAME="Submit" value="Submit"
ONCLICK="javascript:CalcHours(theForm);"></TD>


when i click the submit, no action been taken

may i know did i did anything wrong ?

hoay fern




  Return to Index