Sunday, February 04, 2007

checkDate in Javascript

I know there are lots of implementations of date validation in Javascript out there, but I've just started learning Javascript and wanted to share this implementation with you, so here are my first two little javascript cents:

function checkDate(month, day, year)
{
var monthLength =
new Array(31,28,31,30,31,30,31,31,30,31,30,31);

if (!day || !month || !year)
return false;

// check for bisestile year
if (year/4 == parseInt(year/4))
monthLength[1] = 29;

if (month < 1 || month > 12)
return false;

if (day > monthLength[month-1])
return false;

return true;
}

2 comments:

Thiromi said...

Man, congratulations!

very clear and sharp syntax.

Anonymous said...

Very clear and useful script, but you forgot exception for 100 and 400 years multiple.

I use % function to calculate leap years.

Here's the syntax:
// check for bisestile year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
monthLength[1] = 29;

Have nice time!