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.
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
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
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
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
Copy link to clipboard
Copied
Do you have it enclosed within a cfoutput block?
Copy link to clipboard
Copied
Two of the reasons the code is not working are:
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