Skip to main content
lovewebdev
Inspiring
May 14, 2010
Question

Trying to CFCATCH an ill formatted date

  • May 14, 2010
  • 3 replies
  • 753 views

I need a date entered in this format

05/21/2010

I'm using this to catch if its entered improperly but its skipping the error instead

<cftry>
<cfset  bywhen = DateFormat("#FORM.date#", "mm/dd/yyyy")>
<cfcatch type="any">
  Your date was not formatted properly. Please go back and try again.
</cfcatch>
</cftry>

I also tried this on a cfquery with an incorrect SQL query and It's simply skipping the cfquery but not displaying the friendly message.

So it seems to be working but not displaying the friendly messages.

Could this be an administrator setting issue.

This topic has been closed for replies.

3 replies

Inspiring
May 28, 2010

Two of the reasons the code is not working are:

  • <cfoutput>#DateFormat("31/12/2009",      "mm/dd/yyyy")#</cfoutput> will amend your string,      switching the month and day to result in 12/31/2009. So it will not      generate any error for strings entered as dd/mm/yyyy.
  • Also a numeric entry will be      taken as a numeric representation of a date/time object and will therefore      be converted to a mm/dd/yyyy format. e.g. <cfoutput>#DateFormat(253698, "mm/dd/yyyy")#</cfoutput> will generate 08/06/2594.

Some options:

1.         You may need to test the form data

Is it a date?

10 characters long?

Are the third and sixth characters "/"s?

First 2 characters within range 1-12?

Characters 4 & 5 within range 1-31 (except short months - note Feb in leap years)

thereby ending up with something like

<cfif isDate(form.date) AND len(trim(form.date) EQ 10) AND (mid(form.date,3,1) EQ "/") AND (mid(form.date,6,1) EQ "/") AND isValid("range",mid(form.date,1,2),1,12) AND isValid("range",mid(form.date,4,2),1,31)> (code not tested)

2.         Provide calendar that user can click on to choose date. Date format is then set by you i.e. cfcalendar - default mask mm/dd/yyyy

http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_c_02.html#3798877

3.             Use <cfinput> tag with validate attribute

http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p65.htm#wp1100379

Inspiring
May 14, 2010

Do you have it enclosed within a cfoutput block?

Inspiring
May 14, 2010

dateFormat() is for formatting something that is already a date (hence the name of the function), for just before you output it to a human.  It's not something that should be used for deciding whether a string can be parsed as a date,

There's functions like parseDateTime() and isDate() and isValid() for stuff like that.

--

Adam

lovewebdev
Inspiring
May 14, 2010

The problem I face with those functions is that

01/04

with no year entered will pass which shouldn't

Inspiring
May 14, 2010

Yeah, you're right... I forgot about that.  It sux.

I suppose if I was in your position, I'd validate that I had all three date parts first:

* listLen(s, "/") eq 3)

* reFind("^\d{2}/\d{1,2}/\d{2}")

* etc

and then parse it as a date.

If poss, though, I'd try to not work with strings in that format, I'd try for YYYY-MM-DD instead.

--

Adam