Click to See Complete Forum and Search --> : adding javascript to date field


gelfy
12-02-2003, 08:42 AM
I have a basic question but I am a beginner in Java Script.

I have 2 fields which are From Date and To Date and want to add
JavaScript that the From Date can never be greater than the To date,
they can be equal but the From can not be greater.

jalarie
12-02-2003, 09:24 AM
First, make sure that your input fields have usable names. No spaces. Maybe:

&nbsp;<input ... name="DateF">
&nbsp;<input ... name="DateT">

Then, add a code to your form tag to set off a function to check the dates when the form is submitted:

&nbsp;<form ... onsubmit="return CheckDates">

Finally, create a JavaScript function to pick up those date, compare them, and return appropriately based on the result:

&nbsp;<script type="text/javascript">
&nbsp;&nbsp;function CheckDates() {
&nbsp;&nbsp;&nbsp;DF=document.forms[0].DateF.value;
&nbsp;&nbsp;&nbsp;DT=document.forms[0].DateT.value;
&nbsp;&nbsp;&nbsp;if (DF > DT) {
&nbsp;&nbsp;&nbsp;&nbsp;return false;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;return true;
&nbsp;&nbsp;}
&nbsp;</script>