• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Trying to CFCATCH an ill formatted date

Participant ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

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.

TOPICS
Advanced techniques

Views

623

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

The problem I face with those functions is that

01/04

with no year entered will pass which shouldn't

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

If I was in the OP's position, I'd do something on the form page.  Options include but are not limited to,

cfinput validate="date"

cfinput mask = "99/99/''9999"

cfcalendar

javascript pop up calendars

and various combinations thereof

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 14, 2010 May 14, 2010

Copy link to clipboard

Copied

Do you have it enclosed within a cfoutput block?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 28, 2010 May 28, 2010

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation