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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid date

Status
Not open for further replies.
May 29, 2001
10
GB
Hi All,

I have created a training form and one of the fields is 'date of when the course was taken'.

What I need is for when the user inputs their course date and it is after todays date e.g. 15/07/01, the system returns an error message like 'sorry course start date invalid, can not be after today's date'

Any help would be greatly appreciated.

Regards

Dave
 
Do you want the date validation to be in ColdFusion or Javascript? - tleish
 
To be honest whatever is easiest, at a guess I would of thought a little bit of JS would be simpliest to check - I'm not sure.

Regards

Dave
 
hi droberts2001

Date's are a real pain.

Here's a solution i've used many times over for all of my date gathering needs. I've pasted it here, but do not have the time to go over every line. At any rate, you need a form with a drop down for month, a text box for day, and a drop down for year. Then the submit button should call the function that checks the date. My forms usually have more than just dates, but ive made this function solely check the date, and if all is hunky dory to submit.

The javascript gets the values of each form field, and creates a javascript date. It also creates a javascript date using the CF server's date info. It checks the dates and pops up alerts if there was a problem.

Good luck with your endeavor.
Code:
<html>
<body>
	<form name=&quot;yourFormName&quot;>
	Course date
		<select name=&quot;udcoursemo&quot;>
			<CFLOOP from=&quot;1&quot; to=&quot;12&quot; index=&quot;monthi&quot;><CFSET temp.value = numberformat(monthi, &quot;00&quot;)><CFSET temp.option = monthAsString(monthi)>
				<CFOUTPUT><option value=&quot;#temp.value#&quot;>#temp.option#</CFOUTPUT>
			</CFLOOP>
		</select>
 		<input type=&quot;text&quot; name=&quot;udcourseday&quot; value=&quot;1&quot; size=&quot;2&quot; maxlength=&quot;2&quot;>
		 
		<select name=&quot;udcourseyr&quot;>
			<CFLOOP from=&quot;2001&quot; to=&quot;2010&quot; index=&quot;yeari&quot;>
				<CFOUTPUT><option value=&quot;#yeari#&quot;>#yeari#</CFOUTPUT>
			</CFLOOP>
		</select>
		<input type=&quot;hidden&quot; name=&quot;udcoursedate&quot; value=&quot;x&quot;>
	<input type=&quot;button&quot; onclick=&quot;checkform()&quot; value=&quot; GO &quot;>
	</form>
</body>
</html>
<script language=&quot;javascript&quot;>
<!--
var doc = document.forms[&quot;yourFormName&quot;];
var error = false;
function checkform()
	{
	label1:
		if (doc.udcourseday)
			{

				if(isNaN(doc.udcourseday.value))
					{
						tmpmsg = 'The &quot;day&quot; field may have invalid characters.';
						alert(tmpmsg);
						error = true;
					} else if (doc.udcourseday.value > 31) {
						tmpmsg = 'The &quot;day&quot; field does not contain a valid date.';
						alert(tmpmsg);
						error = true;
					} else {
						<CFOUTPUT>
						var now = new Date(#dateformat(now(),'yyyy')#,#evaluate(dateformat(now(),'mm')-1)#,#dateformat(now(),'dd')#);
						</CFOUTPUT>
						var mo = doc.udcoursemo.options[doc.udcoursemo.options.selectedIndex].value;
						var day = doc.udcourseday.value;
						var yr = doc.udcourseyr.options[doc.udcourseyr.options.selectedIndex].value;

						var coursedate = new Date(yr,mo-1,day);
						var todaysdate = new Date(now);

						doc.udcoursedate.value = mo + '/' + day + '/' + yr ;

					}
			}

			if (coursedate > todaysdate)
				{
				tmpmsg = &quot;Sorry course start date invalid, can not be after today's date.&quot;;

				alert(tmpmsg);

				} else if (error == false) {
					doc.submit();
				}
	}
//-->
</script>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top