Java Script Help Required
properly.When entering some arbit value in the Time1 text box the message
'Time is not in a valid format.' is populated infinitely ... its falling
in a loop ...
plz help me out ...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<Script language = 'javascript'>
function validate()
{
alert("rini is here");
var Time1 = document.add.Time1.value;
validatetime(Time1);
alert("after function");
if(!true)
{
alert("rini");
}
document.add.Add.value = parseInt(document.add.Time1.value) + parseInt(document.add.Time2.value)
alert(document.add.Add.value);
}
function validatetime(S)
{
alert("this is in validatetime");
alert(S);
return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/
}
function IsValidTime(timeStr,timeField) {
// Checks if time is in HH:MM format.
var timePat = /^(\d{1,2}):(\d{2})?$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
timeField.focus();
return false;
}
hour = matchArray[1];
minute = matchArray[2];
if (hour < 0 || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}
if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
return true;
}
function fnTimeAdd()
{
var t1=document.add.Time1.value;
var t2=document.add.Time2.value;
var hh1=(t1.substring(0,t1.indexOf(':'))-0);
var hh2=(t2.substring(0,t2.indexOf(':'))-0);
var mm1= (t1.substring(t1.indexOf(':')+1,t1.length)-0);
var mm2= (t2.substring(t2.indexOf(':')+1,t2.length)-0);
if(hh1 > hh2)
{
alert ("check ur entry");
document.add.Time2.focus();
document.add.Time2.select();
return;
}
if((hh1>24)||(hh2>24)||(mm1>60)||(mm2>60))
{
if((hh1>24)||(hh2>24))
{
alert("Enter hh within 24.");
return;
}
if((mm1>60)||(mm2>60))
{
alert("Enter mm within 60.");
return;
}
}
else
{
var m = (t1.substring(0,t1.indexOf(':'))-0) * 60 +
(t1.substring(t1.indexOf(':')+1,t1.length)-0) +
(t2.substring(0,t2.indexOf(':'))-0) * 60 +
(t2.substring(t2.indexOf(':')+1,t2.length)-0);
var n = (t1.substring(0,t1.indexOf(':'))-0) * 60 +
(t1.substring(t1.indexOf(':')+1,t1.length)-0) -
(t2.substring(0,t2.indexOf(':'))-0) * 60 -
(t2.substring(t2.indexOf(':')+1,t2.length)-0);
alert("N val : "+n);
var h = Math.floor(n / 60);
if(n<0)
{
n =n*(-1);
h = Math.floor(n / 60);
var result=h + ':' + (n - (h * 60));
}
else
{
if(h<0)
{
h=h*(-1);
var result=h + ':' + (n - (h * 60*(-1)));
}
else
{
alert("H val : "+h);
var result=h + ':' + (n - (h * 60));
}
}
alert("The summation Result : "+result);
document.add.Add.value = result;
return result;
}
}
</script>
<body>
<form name = "add" method="POST" action="--WEBBOT-SELF--" >
<!--webbot bot="SaveResults" U-File="C:\_private\form_results.txt"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p><input type="text" name="Time1" size="20" onBlur = 'javascript:IsValidTime(document.add.Time1.value,document.add.Time1);'
>
<!--webbot bot="Validation" S-Data-Type="String" S-Allow-Other-Chars="xx:xx"
-->
<input type="text" name="Time2" size="20" onBlur = 'javascript:IsValidTime(document.add.Time2.value,document.add.Time2);'>
<input type="text" name="Add" size="20" onfocus = 'javascript:fnTimeAdd()'>
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset"
name="B2"></p>
</form>
</body>
</html>

