Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Validating Dates

If you have ever created a web-site that allows a user to enter a date in a field you know that often times what the user enters as a date is not exactly what you expect to receive or visa versa. What exactly do I mean? Well lets take the simple date of 1/12/2008 is this December 1st or January 12th? The right answer is: "It depends". If I live in the united states then the standard date format is mm/dd/yyyy but in Europe and Canada and quite frankly most of the rest of the world, the date format is dd/mm/yyyy or ISO 8601.

Accepting Dates

The method that you choose to accept dates depends largely on your audience, if you can "guarantee" that your audience, meaning the visitors to your site, are going to come only from one country then you can rely on the user to enter the data in that regions format. But if you aren't certain or can't be certain where your visitors come from what do you do?

Dates Selector

There are many variations to accepting dates, one of the common approaches, and one I don't particularly like because of the number of clicks, is to provide 3 dropdown boxes that represent the date in the format you would like to have it. The month box would have the values 1 through 12, the day box 1 through 31, and the year box you could dynamically populate so that it always includes the current year plus 1.
Example:

<select name="year">
   <cfloop from="2001" to="#(year(now())+1)#" index="y">
      <cfoutput><option value="#y#">#y#</option></cfoutput>
   </cfloop>
</select>
This method is still imperfect since it's quite possible to still enter an invalid date e.g. 2/31/2008 (Feb 31,2008). Completely legal with the dropdown method and completely false. So how do you validate this date? You could validate it on the client side using Javascript (which I won't go into here) or you could do it on the server with Coldfusion

The IsDate() Function

Coldfusion provides the java isDate function that will take a date as a string and test to see if it is valid. The isdate function checks the string you pass it and tests to see if it is a date and returns true or false.

<cfset mydate = "#form.month#/#form.day#/#form.year#"/>
<cfif not isDate(mydate)>
   <!--- alert user that they entered an invalid date --->
   <cfoutput>#mydate# is not a valid date</cfoutput>
<cfelse>
   <!--- process the form --->
</cfif>
If as mentioned above the user entered 2/31/2008 as a date they would see the message "2/31/2008 is not a valid date"

Other Methods

So how can you be sure that you will always get the expected date format? I have been pretty successful by using a javascript calendar and a text box. When a user clicks in the text box the popup calendar is displayed and the user must select the date from the calendar, they have no option of typing the date in themselves. If you are worried about usability there are keyboard navigable popup calendars or other means of allowing physically challenged visitors to enter the date and still get it in the right format the first time. This doesn't mean that you shouldn't still double check to make sure you have a valid date.