Javascript Single Date Check Script

30 November -0001

This function checks to make sure that the field named 'date' in the form 'theForm' on the page is in correct 'mm/dd/yyyy' format. Call it with 'return fixit()' in an onSubmit in the form tag. Below this function is another that checks for yyy-mm-dd format a little more elegantly by using regular expressions.

function checkIt()
	{
/* Javascript date check by Justin Klein Keane  Copyright (C) 2004 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
	   
*/
	
	/* Note the date field is named 'date' in the form 'theForm' */
	var theDateString = document.theForm.date.value;
	var LenDateString = document.theForm.date.value.length;
	var monthEndsAt = theDateString.search('/');
	if (monthEndsAt < 2)
		{alert ("Month must be two digits");
		 return false;}
	var LenRestOfString = (LenDateString - monthEndsAt);
	var monthYearString = theDateString.substr(monthEndsAt+1, LenRestOfString);
	var YearBeginsAt = monthYearString.search('/');
	if (YearBeginsAt < 2)
		{alert ("Day must be two digits");
		 return false;}
	
	var endYearAt = (monthYearString.length) - YearBeginsAt + 1;
	var theYearString = monthYearString.substr(YearBeginsAt+1, endYearAt);
	if ((LenDateString < 8) || (LenDateString > 10))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the first slash is in the right place 
	else if ((theDateString.substr(1,1) != "/") && (theDateString.substr(2,1) != "/"))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 alert ("The slashes aren't in the right place");
		 return false;
		}
	// check to make sure the month is not zero
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(0,2) < 1))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the month is not greater than 12
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(0,2) > 12))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the second slash is in the right place
	else if ((monthYearString.substr(1,1) != "/") && (monthYearString.substr(2,1) != "/"))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 alert ("The slashes aren't in the right place");
		 return false;
		}
	// check to make sure the day isn't zero
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(3,2) < 1))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the day isn't greater than 32
	
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(3,2) > 32))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the year is four digits
	else if (theYearString.length != 4)
		{alert ("The year needs to be four digits");
		 return false;
		}
	else
		{
		return true();
		}
	}


function valiDate() {
/* Javascript date check by Justin Klein Keane  Copyright (C) 2004 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
	   
*/
var myRegex = new RegExp("^[0-9]{4}\-([0][0-9]|[1][0-2])\-([0-2][0-9]|[3][0-1])$");
if (document.theForm.date.value.match(myRegex))
	{//valid date format, check for valid date
	var theDay = Math.round(document.theForm.date.value.substr(8,2));
	var theMonth = Math.round(document.theForm.date.value.substr(5,2));
	var theYear = Math.round(document.theForm.date.value.substr(0,4));
	
	if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
		alert ("Not a valid date.");
		return false;
		}
	else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
		alert ("Not a valid date.");
		return false;
		}
	else if ((theDay > 30) && (theMonth == 4 || theMonth == 6 || theMonth == 0 || theMonth == 11)) {
		alert ("Not a valid date.");
		return false;
		}
	else {
		return true;
		}
	}
else
	{
	alert ("Date not in correct yyyy-mm-dd format of the date specified does not exist.");
	return false;
	}
}