Friday, July 23, 2010

Javascript validate date - native

If somehow you are trying to validate dates that involves 2 drop downs and the values in the drop down are set, you can do the following.

function validateDate(dateToCheck, selectedDay)
{
var totalMonthCount = 12;
var i = 1;
var isValid = true;

var monthToCheck = parseInt(dateToCheck.getMonth());
var yearToCheck = parseInt(dateToCheck.getFullYear());

if(selectedDay > 28)
{
if(monthToCheck == 2)
{
if(yearToCheck%4 == 0)
{
if(selectedDay > 29)
{
isValid = false;
}
}
else
{
isValid = false;
}
}

if((monthToCheck == 4) || (monthToCheck == 6) || (monthToCheck == 9) || (monthToCheck == 11))
{
if(selectedDay > 30)
{
isValid = false;
}
}
}

return isValid;
}

2 comments:

  1. does that work for day-light saving?

    ReplyDelete
  2. I am only validating the day, month and year.

    No time checking, unfortunately

    ReplyDelete