you are here: codestackercodes [RSS]simple date validation

simple date validation

function dateValidation() {

	var obj = document.getElementById("my_field"); // my_field: 12/12/2011

	var day = obj.value.split("/")[0];

	var month = obj.value.split("/")[1];
	var year = obj.value.split("/")[2];

	if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4)) {
		alert("Invalid format");
		return false;
	} else {

		var dt = new Date(year, month-1, day);
		var today = new Date();

		if((dt.getDate() != day) || (dt.getMonth() != month-1) || (dt.getFullYear()!=year) || (dt>today)) {
			alert("Invalid date");
			return false;
		}
	}
}

Comments