Tuesday, March 24, 2009

fromdate always less than todate validation using javascript

function DateCompare()
{
objfromDt="12/01/2008";
objtoDt="11/01/2008";
var arrFrom=objfromDt.value.split('/');
var arrTo=objtoDt.value.split('/');
var strFromMonth=arrFrom[0];
var strFromDate=arrFrom[1];
var strToMonth=arrTo[0];
var strToDt=arrTo[1];
if(parseInt(arrFrom[0])<10)
{
strFromMonth=arrFrom[0].replace('0','');
}
if(parseInt(arrFrom[1])<10)
{
strFromDate=arrFrom[1].replace('0','');
}
if(parseInt(arrTo[0])<10)
{
strToMonth=arrTo[0].replace('0','');
}
if(parseInt(arrTo[1])<10)
{
strToDt=arrTo[1].replace('0','');
}
var mon1 = parseInt(strFromMonth);
var dt1 = parseInt(strFromDate);
var yr1 = parseInt(arrFrom[2]);

var mon2 = parseInt(strToMonth);
var dt2 = parseInt(strToDt);
var yr2 = parseInt(arrTo[2]);

var date1 = new Date(yr1, mon1, dt1);
var date2 = new Date(yr2, mon2, dt2);
if(date2 < date1)
{
alert("From date always less than to date");
return false;
}
}