Date Validation in Javascript

Pavanandey
Posted by Pavanandey under JavaScript category on | Points: 40 | Views : 1033
function datechk(datevalue)
{
var year = datevalue.substring(6,datevalue.length);
var month=datevalue.substring(3,5); var day=datevalue.substring(0,2);
var mChk=0; var dChk=0; var curDate=new Date(); var curYear=curDate.getFullYear();
var curMonth=curDate.getMonth(); var curDay=curDate.getDate();
//checking for month
if(eval(month)>12 || eval(month)< 1){alert("Invalid Month,The required format is(DD/MM/YYYY) !!! \n Eg: Date: 01/01/2009 " );
return false;}
//checking for day
if(eval(month)==01 || eval(month)==03 || eval(month)==05 || eval(month)==07 || eval(month)==08 || eval(month)==10 || eval(month)==12){
if(eval(day)>31 || eval(day)<1){
alert(" Invalid Day,The required format is(DD/MM/YYYY) !!! \n Eg: Date: 01/01/2009 ");
return false;}}
else{
if(eval(month)==02)
{ if(isLeap(year))
{ if(eval(day)>29 || eval(day)<1)
{alert(" Invalid Day,The required format is(DD/MM/YYYY) !!! \n Eg: Date: 01/01/2009 "); return false;}
}
else
{ if(eval(day)>28 || eval(day)<1)
{alert(" Invalid Day,The required format is(DD/MM/YYYY) !!! \n Eg: Date: 01/01/2009 ");return false; }
}
}
else{if(eval(day)>30 || eval(day)<1){alert(" Invalid Day,The required format is(DD/MM/YYYY) !!! \n Eg: Date: 01/01/2009 ");return false;}}}
// checking for Year
// var flgDate=0;
// //Same year
// curMonth=curDate.getMonth();
// if(eval(year)==eval(curYear))
// { if(eval(month)>eval(curMonth)+1)
// {flgDate=1;}
// if(eval(month)==eval(curMonth)+1)
// { if(eval(day)>eval(curDay))
// {flgDate=1;}
// }
// }
// if(eval(year)<=1900 || eval(year)>eval(curYear) || flgDate==1){alert("please enter the date >(01/01/1900) and < current date!!!");return false;}
return true;
}

//Is Leapyear
function isLeap(year)
{
if((year/4) != Math.floor(year/4)) return false;
if((year/100) != Math.floor(year/100)) return true;
if((year/400) != Math.floor(year/400)) return false;
return true;
}

Comments or Responses

Login to post response