VB icon

isDate()

Email
Submitted on: 1/5/2015 2:05:00 PM
By: Cecil Denney (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages)
Views: 962
 
     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
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :isDate()
'**************************************
Copyright 2001, The Jesse Tree
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: isDate()
' Description: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
' By: Cecil Denney (from psc cd)
'
' Inputs:a date in numeric form, day/month/year, one or two digits for day and month. Assume 2 digit years are for 2000.
'
' Returns:Corrected, editted date or "ERROR: " message.
'
' Assumes:Makes use of 400 year cycle calendar for leap years.
'**************************************

<% 
'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>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.