How to validate Date using Javascript Regular Expression




Here is a simple Javascript function to validate a date in US format.


function isDateValid(date) {
	//RegEx to validate date
	/*
	* ^[0-1][1-2] : The month starts with a 0 or 1 followed by either a 1 or a 2
	* [- / ]?: Followed by  an optional "-" or "/".
	* (0[1-9]|[12][0-9]|3[01]) : The day part must be either between 01-09, or 10-29 or 30-31.
	* [- / ]?: Day part will be followed by  an optional "-" or "/".
	* (18|19|20|21)\\d{2}$ : Year begins with either 18, 19, 20 or 21 and ends with two digits.
	*/
	var dateRegExPattern = "^[0-1][1-2][- / ]?(0[1-9]|[12][0-9]|3[01])[- /]?(18|19|20|21)\\d{2}$";
	if(date.match(dateRegExPattern)){
		return true;
	}else{
		return false;
	}
	
}