Sunday, November 9, 2008

How to validate date using js

function getValidateDate(){
var tmp=true;

var strFrom=document.getElementById("txtFrom").value;
var strTo=document.getElementById("txtTo").value;

if(strFrom!='' && strTo!=''){
var fdate=strFrom.split('-');

var tdate=strTo.split('-');

var strFDate=new Date();
strFDate.setFullYear(fdate[0],fdate[1],fdate[2]);

var strTDate=new Date();
strTDate.setFullYear(tdate[0],tdate[1],tdate[2]);

if(strTDate<strFDate){
alert(strFrom +" is after "+ strTo);
tmp=false;
}
}else{
alert("Date period must be filled");
tmp=false;
}

return tmp;
}


call the getValidateDate() method as follows in the form.

onsubmit="return getValidateDate()"

No comments:

Post a Comment