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.
The problem I face with those functions is that
with no year entered will pass which shouldn't
Do you have it enclosed within a cfoutput block?
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)
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.
If I was in the OP's position, I'd do something on the form page. Options include but are not limited to,
cfinput mask = "99/99/''9999"
and various combinations thereof
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.
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
3. Use <cfinput> tag with validate attribute