Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I validate a date

Status
Not open for further replies.

BlastRadius

Programmer
Nov 13, 2001
38
CA
ok here it is! I have a form with three jump menus. The menus are populated via a component's methods. This refreshes the pages menu's days field to reflect the number of days in the selected month. The curent selected date is created by combining the value of the 3 fields and is passed as an aurgument to the component. This whole aspect works beautifully. The problem is if the user selects say day "31" when the month is on a month with 31 days and than changes the month to say "February" which has at most 29 days, the dynamicly created date is now invalid and I get an error. How can I validate the date and so I can change the value of the day to "1"?
 
It sounds like you'll need to write some JavaScript that fires as part of the onChange event for the "month" field and updates the "day" field to only have the correct number of days. I can help you with this, but I'll need more information on the names of your form fields, and definitely more information on your statement that "The menus are populated via a component's methods" -- what is the component? Is it a script, or a COM component, or what? A code sample would definitely help me help you.
 
in any case, whether the day of month dropdown can be tweaked to the right month (and leap year), when the form fields arrive at the server you will have to validate the date all over again

actually, that's easier than it sounds

just use cftry/cfcatch and the database will tell you if the date is invalid

rudy
 
Thanks Rudy, I had forgoten about the cftry/cfcatch tags I combined these with a few of the list tags and was able to catch the error and recreate the date as a valid date. Works great. I'll include the code incase anyone else is having the same problem.

<cfif isDefined(&quot;url.date&quot;)>
<cftry>
<cfset strDate = url.date>
<cfset strDate = DateFormat(strDate,&quot;short&quot;)>
<cfcatch type=&quot;any&quot;>
<cfset strDate = listDeleteAt(strDate,2,&quot;/&quot;)>
<cfSet strDate = listInsertAt(strDate,2,&quot;1&quot;,&quot;/&quot;)>
</cfcatch>
</cftry>
<cfelse>
<cfset strDate = DateFormat(now(),&quot;short&quot;)>
</cfif>

pcorreia thanks for the tip but I think I would have still had the same problem doing it with java script. And to answer your question the component is a cold fusion component(cfc).

Thanks

Blast
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top