How to alert the user when one date is earlier than another date using select boxes.

I've searched around and I can't seem to find the right javascript code to validate my form.  What I'm trying to do is show two select boxes populated with date values.  The first is Arrival Date and the second is Departure Date.  What I want to prevent is having the user accidentally put the Departure Date that is before the Arrival Date.

I've tried this javascript but it hasn't worked:

var ADATE = new Date(document.pform.Arrival_Date.value);
var DDATE = new Date(document.pform.Departure_Date.value);
      if(DDATE < ADATE){
      alert("Your Departure Date must be at least one day after your Arrival Date.");
      return false;
      }

----------------------Form fields example----------------------------------------

<select name="Arrival_Date">
                              
<option value="none">--Select Date--</option>
                              
<option value="08-Jun-06">Thu, 8-Jun-06</option>
                                    
<option value="09-Jun-06">Fri, 9-Jun-06</option>
                                    
<option value="10-Jun-06">Sat, 10-Jun-06</option>
                                    
</select>

<select name="Departure_Date">
                              
<option value="none">--Select Date--</option>
                              
<option value="08-Jun-06">Thu, 8-Jun-06</option>
                                    
<option value="09-Jun-06">Fri, 9-Jun-06</option>
                                    
<option value="10-Jun-06">Sat, 10-Jun-06</option>
                                    
</select>
----------------------------------------------------------------------

I'm not sure if the date format is causing a problem.  Any help is appreciated.
LVL 4
Ike23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CKP74Commented:
Try this .
instead of field use your select box for field its work perofect.

<html>


<script>

function Compare() {


if (document.frmTest.startDate.value > document.frmTest.endDate.value){
alert ("Start Date is greater");
}

if (document.frmTest.startDate.value < document.frmTest.endDate.value){
alert ("End Date is greater");
}

if (document.frmTest.startDate.value == document.frmTest.endDate.value){
alert ("Dates are the same");
}


}
</script>


<body>


<form name="frmTest">
     Start Date: <input type="text" name="startDate" size="12">
     <br>
     End Date: <input type="text" name="endDate" size="12">
     <a href="javascript:Compare();">Compare</a>

</form>

</body>

</html>
Regards,
CKP74
0
nabsolCommented:
Hi

Hope this is what you want.

<html>
<head>
 <title>Untitled Page</title>
  <script type="text/javascript">
function a()
{
   
   date1 = document.pform.Arrival_Date.value
   date2 = document.pform.Departure_Date.value

   date1 = date1.split("-")
   date2 = date2.split("-")

   var ADATE = new Date(date1[2], themonth(date1[1]), date1[0]);
   var DDATE = new Date(date2[2], themonth(date2[1]), date2[0]);
   if(DDATE < ADATE){
     alert("Your Departure Date must be at least one day after your Arrival Date.");
     return false;
   }

}

function themonth(thismonth)
{
   allmonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
   for(i=0; i <= 11; i++)
    if(thismonth == allmonths[i])
       return i;
}

</script>

</head>
<body>
<form name="pform" onsubmit="return a()">
<select name="Arrival_Date">
                         
<option value="none">--Select Date--</option>
                         
<option value="08-Jun-2006">Thu, 8-Jun-06</option>
                             
<option value="09-Jun-2006">Fri, 9-Jun-06</option>
                             
<option value="10-Jun-2006">Sat, 10-Jun-06</option>
                             
</select>

<select name="Departure_Date">
                         
<option value="none">--Select Date--</option>
                         
<option value="08-Jun-2006">Thu, 8-Jun-06</option>
                             
<option value="09-Jun-2006">Fri, 9-Jun-06</option>
                             
<option value="10-Jun-2006">Sat, 10-Jun-06</option>
                             
</select>

<input type="submit" id="button1" value="check">
</body>
</html>

By Nab
0
nabsolCommented:
Hi

OR directly use this:

<html>
<head>
 <title>Untitled Page</title>
  <script type="text/javascript">
function a()
{
   
   date1 = document.pform.Arrival_Date.value
   date2 = document.pform.Departure_Date.value

   date1 = date1.split("-")
   date2 = date2.split("-")

   var ADATE = new Date(date1[2], date1[1], date1[0]);
   var DDATE = new Date(date2[2], date2[1], date2[0]);
   if(DDATE < ADATE){
     alert("Your Departure Date must be at least one day after your Arrival Date.");
     return false;
   }

}


</script>

</head>
<body>
<form name="pform" onsubmit="return a()">
<select name="Arrival_Date">
                         
<option value="none">--Select Date--</option>
                         
<option value="08-01-2006">Thu, 8-Jun-06</option>
                             
<option value="09-01-2006">Fri, 9-Jun-06</option>
                             
<option value="10-01-2006">Sat, 10-Jun-06</option>
                             
</select>

<select name="Departure_Date">
                         
<option value="none">--Select Date--</option>
                         
<option value="08-01-2006">Thu, 8-Jun-06</option>
                             
<option value="09-01-2006">Fri, 9-Jun-06</option>
                             
<option value="10-01-2006">Sat, 10-Jun-06</option>
                             
</select>

<input type="submit" id="button1" value="check">
</body>
</html>

By Nab
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ike23Author Commented:
This didn't work:
   date1 = document.pform.Arrival_Date.value
   date2 = document.pform.Departure_Date.value

   date1 = date1.split("-")
   date2 = date2.split("-")

   var ADATE = new Date(date1[2], date1[1], date1[0]);
   var DDATE = new Date(date2[2], date2[1], date2[0]);
   if(DDATE < ADATE){
     alert("Your Departure Date must be at least one day after your Arrival Date.");
     return false;
   }

I noticed you changed the date values to all numbers.  How would the system know that the date was supposed to be formatted DD-MM-YYYY?  I noticed the other function in the first post but I don't see where it is used:

function themonth(thismonth)
{
   allmonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
   for(i=0; i <= 11; i++)
    if(thismonth == allmonths[i])
       return i;
}

Thanks!
0
Ike23Author Commented:
Actually, I got it to work by changing the year to 4 digits instead of 2 digits.

var ADATE = document.pform.Arrival_Date.value;
var DDATE = document.pform.Departure_Date.value;
            if(DDATE < ADATE){
            alert("Your Departure Date must be at least one day after your Arrival Date.");
            return false;
            }

I used my same javascript code but I guess I will give the points because I noticed the 4 digit year in your example.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.