...

View Full Version : How to validate time where to-time must be greater than from-time.



narinas
01-19-2012, 04:32 AM
hi im using dhtml xgrid where i need to do validation for time ,(from-time and to-time i.e ind==1 and ind==2 ) to-time must be greater than from-time else i need to alert a message to-time must be greater than from-time.
can any one send me the code for it time format is[05:00].

var err_str="";
function validate_grid(value,id,ind)
{
$("#result").html(" ").show();
if(ind==1 || ind==2)
{
var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
if(!patt.test(value))
{
mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
if(err_str!="")
err_str+="\n"+(ind+1)+".Enter numbers and : only..";
else
err_str=(ind+1)+".Enter numbers and : only";
return false;
}
else
{
mygrid.setCellTextStyle(id,ind,"background-color:white;");
return true;
}
}
}

Philip M
01-19-2012, 08:26 AM
Double-posted :( http://www.codingforums.com/showthread.php?t=249183

Please look at forum rule #1.2



<script type = "text/javascript">

var fromTime = "05:50"; // format hh:mm
var toTime = "06:21";
var ft = fromTime.split(":");
var tt = toTime.split(":");
var OK = false;
if (tt[0] > ft[0]) {OK=true}
if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
if (OK) {
alert ("The second time is after the first time");
var hr = tt[0] - ft[0];
var mn = tt[1] - ft[1];
if (mn < 0) {mn= mn+60; hr = hr-1}
if (mn <10) {mn = "0" + mn}
alert ("Difference is " + hr + ":" + mn);
}
else {alert ("The second time is NOT after the first time")}

</script>

"The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson

narinas
01-19-2012, 12:03 PM
Double-posted :( http://www.codingforums.com/showthread.php?t=249183

Please look at forum rule #1.2



<script type = "text/javascript">

var fromTime = "05:50"; // format hh:mm
var toTime = "06:21";
var ft = fromTime.split(":");
var tt = toTime.split(":");
var OK = false;
if (tt[0] > ft[0]) {OK=true}
if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
if (OK) {
alert ("The second time is after the first time");
var hr = tt[0] - ft[0];
var mn = tt[1] - ft[1];
if (mn < 0) {mn= mn+60; hr = hr-1}
if (mn <10) {mn = "0" + mn}
alert ("Difference is " + hr + ":" + mn);
}
else {alert ("The second time is NOT after the first time")}

</script>

"The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson

how to use the code in the above format as from_time and to_time is ind==1 and ind==2

Philip M
01-19-2012, 12:18 PM
how to use the code in the above format as from_time and to_time is ind==1 and ind==2

Obviously if you are going to compare two times, both of the times must be available. Your validation script checks the format of one or the other time, according to ind == 1 or ind == 2.

You need to wrap the script I gave you within a function, and call it when the format of both the times has been validated. Or re-write the validation script. I sense that you don't know much Javascript and may perhaps find that difficult. This is an example of why it is often difficult to adapt pre-written code to something it was not originally designed to do.

narinas
01-19-2012, 12:25 PM
if i give from time as "6:00" to time as "08:00" then its showing wrong.

Philip M
01-19-2012, 01:38 PM
if i give from time as "6:00" to time as "08:00" then its showing wrong.

The time format is supposed to be hh:mm with leading zeroes (06:00). That is the proper way to show 24-hour clock time, and is the pattern required by your validation - var patt=/^([0][0-9]
If you want to allow times with no leading zeroes (6:00), then



<script type = "text/javascript">

var fromTime = "06:00"; // format hh:mm
var toTime = "08:00";
var ft = fromTime.split(":");
var tt = toTime.split(":");
var OK = false;
if (tt[0]*1 > ft[0]*1) {OK=true}
if ((tt[0]*1 == ft[0]*1) && (tt[1]*1 > ft[1]*1)) {OK = true}
if (OK) {
alert ("The second time is after the first time");
var hr = tt[0] - ft[0];
var mn = tt[1] - ft[1];
if (mn < 0) {mn= mn+60; hr = hr-1}
if (mn <10) {mn = "0" + mn}
alert ("Difference is " + hr + ":" + mn);
}
else {alert ("The second time is NOT after the first time")}

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum