Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-20
5
Medium Priority
?
201 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:Ike23
  • 2
  • 2
5 Comments
 

Expert Comment

by:CKP74
ID: 16501968
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
 
LVL 6

Expert Comment

by:nabsol
ID: 16502237
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
 
LVL 6

Accepted Solution

by:
nabsol earned 2000 total points
ID: 16502249
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
 
LVL 4

Author Comment

by:Ike23
ID: 16502682
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
 
LVL 4

Author Comment

by:Ike23
ID: 16502812
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question