PDA

View Full Version : Help With Validation on Jason's Date Input Calendar



NGJ
05-22-2006, 06:55 PM
Hi,
I'm using Jason's Date Input Calendar, within a Form, to display the current date and to allow the user to select a 'response by' date. I want the validation to alert the user if the second date they've selected is in the past.

The following code achieves this, but upon clicking 'ok' to the pop-up alert box - the Form posts regardless.

Can anyone help please? :confused:

The basic code is as follows:

<html>
<head>
<script type="text/javascript" src="calendarDateInput.js">
</script>
</head>

<body>

<script type="text/javascript">
function validateTheDate() {
var Today = new Date();
if (Response_Requested_By_Object.picked.date < Today) alert('Cannot select a date in the past.');
else if (Response_Requested_By_Object.picked.yearValue > 2020) alert('Cannot select dates beyond 2020.');
}
</script>


<form method="post" action="../mailing.asp" Name="Form" onSubmit="validateTheDate();">

<table>
<tr>
<td colspan="2">Date Requested</td>
<td><script>DateInput('CCN_Request_Date', true, 'DD-MON-YYYY')</script></td>
</tr>
<tr>
<td colspan="2">Response Requested By</td>
<td><script>DateInput('Response_Requested_By',true, 'DD-MON-YYYY')</script></td>
</tr>
</table>

<table>
<td><input type="Submit" value="Finish!"></td>
</table>
</form>
</body>
</html>

jasonmoon
05-22-2006, 07:58 PM
When calling a function from the onSubmit event-handler of a form, if you don't want the form to submit, you must return FALSE from that function.

So, you'd want to change the following:

1) Put the "return" keyword in front of your onSubmit function call:

<form ... onSubmit="return validateTheDate()">

2) Make sure to return false when they screw up the date:

function validateTheDate() {

var dateOK = false;
var Today = new Date();
if (Response_Requested_By_Object.picked.date < Today) alert('Cannot select a date in the past.');
else if (Response_Requested_By_Object.picked.yearValue > 2020) alert('Cannot select dates beyond 2020.');
else dateOK = true;
return dateOK;

}

NGJ
05-22-2006, 08:37 PM
Nice one.

All sorted... and it makes sense!!

Good looking script - Thanks for your help!! :D

mahathi
07-24-2006, 10:46 AM
Hi
It worked for me yesteday. But today when I again started debugging the project it is not working. What can be the case. Will you please help me?
Regards
Mahathi.