# 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

Original Author: Cecil Denney

### Inputs

a date in numeric form, day/month/year, one or two digits for day and month. Assume 2 digit years are for 2000.

### Assumptions

Makes use of 400 year cycle calendar for leap years.

### Returns

Corrected, editted date or "ERROR: " message.

### Code

`<% '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 77550option explicitdim i, Date1, Date2%><br/>// The IsDate() a short, clean JavaScript function that uses the 400 year cycle <br/>//&nbsp;&nbsp;calendar to validate a date.<br/>// It accepts one or two digits for the day, month, or year. However, for the year,<br/>//&nbsp;&nbsp;two digits are assumed to be 2000+<br/>// CheckDate() uses isDate() to check the date and display the result or an error message.<br/>function isDate(theDate)<br/>{<br/> var dtval = theDate.split("/")<br/> if (dtval.length != 3) {return "ERROR: Format is 'mm/dd/yyyy'" }<br/> else { <br/> var mo = dtval[0]-1+1;<br/> var day = dtval[1]-1+1;<br/> var yr = dtval[2]-1+1;<br/> yr = (yr < 100) ? yr + 2000 : yr;<br/> if (yr > 9999){return "ERROR: Maximum year 9999."}<br/> var days = 0<br/> if (mo >= 13){ return "ERROR: Maximum 12 months." } <br/> &nbsp;&nbsp;&nbsp;&nbsp;else {<br/> switch (mo)<br/> {<br/> case 2:<br/> // for February (implements 400 year cycle calculation)<br/> days = (((yr%4 == 0 && yr%100 != 0) || yr%400 ==0) ? 29 : 28 );<br/> break;<br/> default:<br/> // for January thru December except February<br/> days = 30 + ((mo < 8) ? mo%2 : (mo%7)%2);<br/> &nbsp;&nbsp; }<br/> if (days < day){return "ERROR: Maximum of "+days+" for month "+mo+"."}<br/> else {return mo+"/"+day+"/"+yr}<br/> //return dtval[0]-1+1<br/> } <br/> &nbsp;&nbsp; }<br/>}<br/>function CheckDate(){<br/>var indate = document.all.Users.value;<br/>indate = isDate(indate);<br/>document.all.Calculated.value = indate;<br/>}<br/>Enter Date: Calculated: `

Posted: 2002-06-01
By: ArchiveBot
Viewed: 117 times

Categories

ASP/ HTML

Attachments

No attachments for this post