Advertisement
ASP_Volume2 Validation/ Processing #42219

isDate()

1) Validate a date entered in a text box without having to use multiple boxes for day, month, year. 2) Implement a short, mathematical method 3) Provide feedback about incorrect entries

AI

AI Samenvatting: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Broncode
original-source
<% 
'copyright 2001, The Jesse Tree 
'This Web page illustrates the use of the date functions
' Developed by Cecil Denney, Automation consultant to 
' The Jesse Tree, 2622 Market Street, Galveston, TX 77550
option explicit
dim i, Date1, Date2
%>
<html>
<head>
<Script Language="JavaScript">
// The IsDate() a short, clean JavaScript function that uses the 400 year cycle 
//  calendar to validate a date.
// It accepts one or two digits for the day, month, or year. However, for the year,
//  two digits are assumed to be 2000+
// CheckDate() uses isDate() to check the date and display the result or an error message.
function isDate(theDate)
{
	var dtval = theDate.split("/")
	if (dtval.length != 3) {return "ERROR: Format is 'mm/dd/yyyy'" }
	else { 
			var mo = dtval[0]-1+1;
			var day = dtval[1]-1+1;
			var yr = dtval[2]-1+1;
			yr = (yr < 100) ? yr + 2000 : yr;
			if (yr > 9999){return "ERROR: Maximum year 9999."}
			var days = 0
			if (mo >= 13){ return "ERROR: Maximum 12 months." } 
	    else {
					switch (mo)
					{
					case 2:
						// for February (implements 400 year cycle calculation)
						days = (((yr%4 == 0 && yr%100 != 0) || yr%400 ==0) ? 29 : 28 );
						break;
					default:
						// for January thru December except February
						days = 30 + ((mo < 8) ? mo%2 : (mo%7)%2);
			   	}
					if (days < day){return "ERROR: Maximum of "+days+" for month "+mo+"."}
					else {return mo+"/"+day+"/"+yr}
					//return dtval[0]-1+1
				 }	
	   }
}
function CheckDate(){
var indate = document.all.Users.value;
indate = isDate(indate);
document.all.Calculated.value = indate;
}
</Script>
</head>
<body>
Enter Date: <input id="Users" type="text" name="TestDate" size="30" onblur="CheckDate()"><br>
Calculated: <input id="Calculated" type="text" name="Calculated" size="40">
</body>
</html>
Originele reacties (3)
Hersteld van de Wayback Machine