Hi Michael,
Here is a custom tag I wrote that does exactly what you want to do. We have been
using it in production for 2 years now without any dramas.
It prevents an invalid date being submitted by using javascrfipt to dynamically limit
the choices based on year/month.
HTH,
Steve
<cfparam name="attributes.defaultDay" default="-1">
<cfparam name="attributes.defaultMonth" default="-1">
<cfparam name="attributes.defaultYear" default="-1">
<cfparam name="attributes.dayName" default="DAY">
<cfparam name="attributes.monthName" default="MONTH">
<cfparam name="attributes.yearName" default="YEAR">
<cfparam name="attributes.yearsShow" default="5">
<cfparam name="attributes.yearsBack" default="Y">
<cfparam name="attributes.yearsEitherSide" default="N">
<cfparam name="attributes.yearsForward" default="N">
<cfparam name="attributes.yearsStart" default="">
<cfparam name="attributes.formName" default="Date">
<cfparam name="attributes.showJS" default="Y">
<cfparam name="attributes.showDay" default="Y">
<cfparam name="attributes.required" default="Y">
<cfif attributes.showJS is "Y">
<script language="JavaScript" type="text/javascript">
function changeDay(day,month,year) {
if ( month.options[month.selectedIndex].value != "-1"
|| year.options[year.selectedIndex].value != "-1" ){
if ((month.selectedIndex == 1) ||
(month.selectedIndex == 3) || (month.selectedIndex == 5) || (month.selectedIndex == 7)
|| (month.selectedIndex == 8) || (month.selectedIndex == 10) || (month.selectedIndex
== 12)) {
day.length = 32;
day.options[0].text = "DAY";
day.options[0].value = "-1";
for (i=1;i < day.length; i++) {
day.options[i].text = i;
day.options[i].value = i;
}
} else if ((month.selectedIndex == 4) ||
(month.selectedIndex == 6) || (month.selectedIndex == 9) || (month.selectedIndex ==
11)) {
day.length = 31;
day.options[0].text = "DAY";
day.options[0].value = "-1";
for (i=1;i < day.length; i++) {
day.options[i].text = i;
day.options[i].value = i;
}
} else if (month.selectedIndex == 2) {
day.length =
dayLength(year.options[year.selectedIndex].value);
day.options[0].text = "DAY";
day.options[0].value = "-1";
for (i=1;i < day.length; i++) {
day.options[i].text = i;
day.options[i].value = i;
}
}
}
}
function dayLength(value){
var length;
if (value % 4 != 0)
length = 29;
else if (value % 400 == 0)
length = 30;
else if (value % 100 == 0)
length = 29;
else
length = 30;
return length;
}
</script>
</cfif>
<cfscript>
if (attributes.showDay eq "N"){
onchangeCall = "N";
}else{
onchangeCall = "Y";
}
if (attributes.showDay eq "N"){
DayStr = "<input type=""hidden"" name=""#attributes.dayName#""
value=""#attributes.defaultDay#"" />";
}else{
DayStr = "<select name=""#attributes.dayName#"" size=""1"">";
DayStr = DayStr & "<option value=""-1"">DAY</option>";
Counter=0;
for (Counter=1; Counter LTE 31; Counter=Counter + 1 ){
DayStr = DayStr & "<option value=""" & Counter & """";
if (attributes.defaultDay eq Counter) {
DayStr = DayStr & " selected=""selected""";
}
DayStr = DayStr & ">" & Counter & "</option>";
}
DayStr = DayStr & "</select>";
}
// now month box
MonthArray = ArrayNew(1);
MonthArray[1] = "Jan";
MonthArray[2] = "Feb";
MonthArray[3] = "Mar";
MonthArray[4] = "Apr";
MonthArray[5] = "May";
MonthArray[6] = "Jun";
MonthArray[7] = "Jul";
MonthArray[8] = "Aug";
MonthArray[9] = "Sep";
MonthArray[10] = "Oct";
MonthArray[11] = "Nov";
MonthArray[12] = "Dec";
MonthStr = "<select name=""#attributes.monthName#""";
if (onchangeCall eq "Y"){
MonthStr = MonthStr & "
onchange=""changeDay(document.#attributes.formName#.#attributes.dayName#,document.#attributes.formName#.#attributes.monthName#,document.#attributes.formName#.#attributes.yearName#)""";
}
MonthStr = MonthStr & " size=""1"">";
MonthStr = MonthStr & "<option value=""-1"">MONTH</option>";
for (monthcounter = 1; monthcounter LTE 12; monthcounter =
monthcounter + 1){
MonthStr = MonthStr & "<option value=""" & monthcounter & """";
if (attributes.defaultMonth eq monthcounter) {
MonthStr = MonthStr & " selected=""selected""";
}
MonthStr = MonthStr & ">" & MonthArray[monthcounter] &
"</option>";
}
MonthStr = MonthStr & "</select>";
// Now Year Box
YearArray = ArrayNew(1);
if (attributes.yearsBack is "Y"){
if (attributes.yearsStart neq "" and
isNumeric(attributes.yearsStart)) {
yearno = attributes.yearsStart;
}else{
yearno = Datepart("yyyy",now());
}
for (i = 1; i LTE attributes.yearsShow; i = i + 1){
YearArray[i] = yearno;
yearno = yearno - 1;
}
}else if (attributes.yearsEitherSide is "Y"){
yearno = Datepart("yyyy",now()) - (Round(attributes.yearsShow
/ 2));
for (i = 1; i LTE attributes.yearsShow; i = i + 1){
YearArray[i] = yearno;
yearno = yearno + 1;
}
}else if (attributes.yearsForward is "Y"){
if (attributes.yearsStart neq "" and
isNumeric(attributes.yearsStart)) {
yearno = attributes.yearsStart;
}else{
yearno = Datepart("yyyy",now());
}
for (i = 1; i LTE attributes.yearsShow; i = i + 1){
YearArray[i] = yearno;
yearno = yearno + 1;
}
}else{
yearno = Datepart("yyyy",now());
for (i = 1; i LTE attributes.yearsShow; i = i + 1){
YearArray[i] = yearno;
yearno = yearno - 1;
}
}
YearStr = "<select name=""#attributes.YearName#""";
if (onchangeCall eq "Y"){
YearStr = YearStr & "
onchange=""changeDay(document.#attributes.formName#.#attributes.dayName#,document.#attributes.formName#.#attributes.monthName#,document.#attributes.formName#.#attributes.yearName#)""";
}
YearStr = YearStr & " size=""1"">";
YearStr = YearStr & "<option value=""-1"">YEAR</option>";
for (j = 1; j LTE ArrayLen(YearArray); j = j + 1){
YearStr = YearStr & "<option value=""" & YearArray[j] & """";
if (attributes.defaultYear eq YearArray[j]) {
YearStr = YearStr & " selected=""selected""";
}
YearStr = YearStr & ">" & YearArray[j] & "</option>";
}
YearStr = YearStr & "</select>";
if (attributes.showDay eq "Y"){
WriteOutput( YearStr & " / " & MonthStr & " / " & DayStr );
}else{
WriteOutput( YearStr & " / " & MonthStr & DayStr);
}
</cfscript>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephen Bosworth
Application Development and Integration
Communication and Information Services
The University of Newcastle, Australia
Phone: 02 4921 6574
Fax: 02 4921 7087
Email: [EMAIL PROTECTED]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> [EMAIL PROTECTED] 6/12/2003 12:30:28 am >>>
Thanks Graham. If this was my site that's what I'd do. But it's not my
site, and the form has been defined by the client. Their reaction to all my
suggestions so far has been "thanks for the suggestions. Good idea. Now do
it how I told you."
So with this client I've given up suggesting stuff. I just do the job I've
been asked and no more. That suits them, that's how they want it to be.
For this job, looks like it's server side validation using "isdate()" when I
check the validity of everything else in the form too.
Cheers,
Michael Kear
Windsor, NSW, Australia
AFP Webworks.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Graham Cook
Sent: Saturday, 6 December 2003 12:01 AM
To: CFAussie Mailing List
Subject: [cfaussie] Re: Easy validation of dates?
Why not just use a calendar custom tag? Macromedia Exchange has a nice
free one called PopDate V2.
---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to
MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004
---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004