Welcome to the fourth part of an eight-part series on constructing a web page calendar. In the previous part, we discussed the first four code segments for the ShowCurrentMonth function of this application. In this part, we will tackle the last three code segments, move on to the validateDate function, and make modifications that will let you accurately display any month of any year.
Showing Any Month with a Web Page Calendar - The validateDate() Function (Page 2 of 5 )
If you set the year, month and number for the day of the month for a date object, JavaScript will not know if that combination is valid. For example, the date, 02/29/2007 does not exist. This says February 29, 2007. The year 2007 is not a leap year, so February 2007 end on the 28th and not the 29th. JavaScript cannot check this. We need to write a function that will check that.
A function to validate dates is not straightforward. Understanding it is a subject worthy of an entire article. Here I will simple tell you what enters the function and what goes out of it. You will need to consult some other document for the explanation of the function that I will give you.
The function here receives as arguments the year, the month and the number of the day of the month. It verifies whether the combination is possible. If the combination is possible, it returns true; if it is not, it returns false.
This is the function:
{
var noMonthDays = new Array(12);
noMonthDays[0] = 31;
noMonthDays[1];
noMonthDays[2] = 31;
noMonthDays[3] = 30;
noMonthDays[4] = 31;
noMonthDays[5] = 30;
noMonthDays[6] = 31;
noMonthDays[7] = 31;
noMonthDays[8] = 30;
noMonthDays[9] = 31;
noMonthDays[10] = 30;
noMonthDays[11] = 31;
if ((year%4)!=0)
noMonthDays[1] = 28;
else if ((year%400)==0)
noMonthDays[1] = 29;
else if ((year%100)==0)
noMonthDays[1] = 28;
else
noMonthDays[1] = 29;
if ((year.length = 4)&&(no<=noMonthDays[month])&&(no>=1))