Pages

Search

Tuesday, October 28, 2008

Java script function to validate from and to dates?

fid indicates from date text box id and tid is to date text box id
function DateLsEq(fid,tid)
{
var msg="From date is greater than To date";
try
{
var fval=document.getElementById(fid).value;
var tval=document.getElementById(tid).value;
if(fval=='' && tval=='')
{
return true;
}
var fdts=null;
var tdts=null;
if(fval!='')
{
if(fval.indexOf(".")!=-1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(fid).focus();
return false;
}
fdts=fval.split("/");

}
if(tval!='')
{
if(tval.indexOf(".")!=-1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(tid).focus();
return false;
}
tdts=tval.split("/");
}
if(tdts!=null)
{
if(tdts[1].length==1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(tid).focus();
return false;
}
if(tdts[0].length==1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(tid).focus();
return false;
}
if(!isInteger(tdts[2]) || !isInteger(tdts[1])|| !isInteger(tdts[0]))
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(tid).focus();
return false;
}
if(!IsValidDate(tval))
{
document.getElementById(tid).focus();
return false;
}
}
if(fdts!=null)
{
if(fdts[1].length==1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(fid).focus();
return false;
}
if(fdts[0].length==1)
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(fid).focus();
return false;
}
if(!isInteger(fdts[2]) || !isInteger(fdts[1]) || !isInteger(fdts[0]))
{
alert('--Not a valid Date(dd/mm/yyyy)');
document.getElementById(fid).focus();
return false;
}
if(!IsValidDate(fval))
{
document.getElementById(fid).focus();
return false;
}
}
if(tdts!=null && fdts!=null)
{
if(tdts[2]<=fdts[2])
{
if(tdts[2]<fdts[2])
{
alert(msg);
document.getElementById(fid).focus();
return false;
}
if(tdts[1]<=fdts[1])
{
if(tdts[1]<fdts[1])
{
alert(msg);
document.getElementById(fid).focus();
return false;
}

if(tdts[0]<fdts[0])
{
alert(msg);
document.getElementById(fid).focus();
return false;
}

}
}
}
return true;
}
catch(e)
{
alert('--Not a valid Date(dd/mm/yyyy)');
return false;
}
}

No comments:

Post a Comment