sunakshi saxena

sunakshi saxena

  • NA
  • 54
  • 82.7k

Time comparison

Mar 14 2012 7:20 AM
Hi i have a java script function which compares two times time in and time out and if time in is greater then time out then it shows a popup that time out should not less then time in but if time in value is greater then 10:00 then its not showing popup because both time-in and time-out are in string and when it compares both then it compare the first value of time-in like 1 with 9 if time out is 9:07 and we give time out value 09:07 then it is perfect i am writing over here my function please suggest me changes in code. as soon as possible.



function IsValidTime(timeStr, strvar)
 {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
var txttimein =(document.getElementById("<%=txtTimeIn.ClientID %>");
var txttimeout = document.getElementById("<%=txtTimeOut.ClientID %>");
 

if (txttimein.value=="")
{
$("#<%=Label1.ClientID%>").text("Time In can not be left blank");
controlID=txttimein;
showDialog();
return false;
}
if (txttimeout.value=="")
{
$("#<%=Label1.ClientID%>").text("Time Out can not be left blank");
controlID=txttimeout;
showDialog();
return false;
}
if (txttimein.value > txttimeout.value)
{
alert(txttimein.value);
$("#<%=Label1.ClientID%>").text("Time Out can not be less than Time In.");
 controlID = txttimein;
 showDialog();
 return false;
}

var timePat =/^(([0-9])|([0-1][0-9])|([2][0-3])):(([0-9])|([0-5][0-9]))$/;

var matchArray = timeStr.match(timePat);

if (matchArray == null) {

 if(strvar == "timein")
 {

  $("#<%=Label1.ClientID%>").text("Time In is not in a valid format.Time format should be HH:MM.");
  controlID = txttimein;
  alert('hi');

  alert("Time In is not in a valid format.Time format should be HH:MM.");
  }
  else
  {
  $("#<%=Label1.ClientID%>").text("Time Out is not in a valid format.  Time format should be HH:MM.");
  alert('hi');
  controlID = txttimeout;
  alert("Time In is not in a valid format.Time format should be HH:MM.");

  }
  showDialog();
 
 
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];


if (second=="") { second = null; }


if (minute<0 || minute > 59) {
$("#<%=Label1.ClientID%>").text("Minute must be between 0 and 59.");
if(strvar == "timein")
 {
 controlID = txttimein;
 
  }
  else
  {
  controlID = txttimeout;
  }
  showDialog();

return false;
}


if (second != null && (second < 0 || second > 59)) {
$("#<%=Label1.ClientID%>").text("Second must be between 0 and 59.");
if(strvar == "timein")
 {
 controlID = txttimein;
 
  }
  else
  {
  controlID = txttimeout;5
  }
  showDialog();

return false;
}
return true;
}

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}