Feb
26
2008

Javascript for validating a date

Javascript for validating a date.

Pass month, day, year as parameters. This function will return true if the date is valid.

// Author: arunmvishnu.com
function validDate(month, day, year){
	if(month =='0' || day =='0' || year=='0'){
		return false;
	}

	switch(month){
		case '1': //** jan
		case '3': //** March
		case '5': //** May
		case '7': //** July
		case '8': //** Aug
		case '10': //** Nov
		case '12': //** Dec
	
		if(day >=1 && day <= 31){
			return true;
		} else {
			return false;
		}
	
		case '4': //** APRIL
		case '6' : //** JUNE
		case '9' : //** SEPTEMBER
		case '11' : //** NOVEMBER
		if(day >=1 && day <= 30){
			return true;
		} else {
			return false;
		}
	
		case '2' : // Feb
		if(isLeapYear(year)){
			if(day >=1 && day <= 29){
				return true;
			}
		} else if(day >=1 && day <= 28){
			return true;
		} else {
			return false;
		}
		default:
			return false;
	}
}

function isLeapYear(year){  // Checking wdr a year is leap year or not
	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)){
		return true;
	} else {
		return false;
	}
}

The full js file can be dodnloaded from here

Related Posts

Author: Arun Vishnu

Leave a comment

Archives

Highslide for Wordpress Plugin