Connecting Tech Pros Worldwide Help | Site Map

From Date & To Date Validation in javascript

  #1  
Old July 3rd, 2009, 10:49 AM
Newbie
 
Join Date: Jun 2009
Posts: 29
I want to validate for From Date & To Date (ie., From Date should not be greater than To Date & also From Date & To Date should not be greater than current date) & Dates are in us format
  #2  
Old July 3rd, 2009, 10:56 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: From Date & To Date Validation in javascript


Show your code. An easy way to compare is to get two Date objects and compare them:
Expand|Select|Wrap|Line Numbers
  1. if (fromDate > toDate) {
  2.     alert("Error! ...");
  3.     // whatever you want to do here
  4. }
So get your date inputs into a Date object - see this link for more details.
  #3  
Old July 3rd, 2009, 12:09 PM
Newbie
 
Join Date: Jun 2009
Posts: 29

re: From Date & To Date Validation in javascript


Thanks for the suggestion acoder it really helped me a lot
  #4  
Old July 3rd, 2009, 12:23 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: From Date & To Date Validation in javascript


No problem. Perhaps you could post your working code for the benefit of others.
  #5  
Old July 3rd, 2009, 12:39 PM
Newbie
 
Join Date: Jun 2009
Posts: 29

re: From Date & To Date Validation in javascript


This is the code which i developed & the date will be in us date

Expand|Select|Wrap|Line Numbers
  1. var objFromDate = document.getElementById("fromdate").value;
  2. var objToDate = document.getElementById("todate").value;
  3.  
  4. var date1 = new Date(objFromDate);
  5. var date2 = new Date(objToDate);
  6.  
  7. var date3 = new Date();
  8. var date4 = date3.getMonth() + "/" + date3.getDay() + "/" + date3.getYear();
  9. var currentDate = new Date(date4);
  10.  
  11.     if(date1 > date2)
  12.     {
  13.         alert("fromdate should be less than todate");
  14.         return false; 
  15.     }
  16.     else if(date1 > currentDate)
  17.     {
  18.         alert("From Date should be less than current date");
  19.         return false; 
  20.     }
  21.                else if(date2 > currentDate) 
  22.                {
  23.                    alert("To Date should be less than current date");
  24.         return false; 
  25.                 }

Last edited by Dormilich; July 3rd, 2009 at 12:50 PM. Reason: Please use [code] tags when posting code
  #6  
Old July 3rd, 2009, 02:16 PM
Newbie
 
Join Date: Jun 2009
Posts: 29

re: From Date & To Date Validation in javascript


How can i edit the above code b'coz there is a little issue in current date. I want to add the new code which works perfectly for my requirement
  #7  
Old July 3rd, 2009, 02:21 PM
Newbie
 
Join Date: Jun 2009
Posts: 29

re: From Date & To Date Validation in javascript


This is the code for date comparison

Expand|Select|Wrap|Line Numbers
  1. var objFromDate = document.getElementById("fromdate").value; 
  2. var objToDate = document.getElementById("todate").value;
  3.  
  4. var FromDate = new Date(objFromDate);
  5.     var ToDate = new Date(objToDate);
  6.     var valCurDate = new Date();
  7.     valCurDate = valCurDate.getMonth()+1 + "/" + valCurDate.getDate() + "/" + valCurDate.getYear();
  8.     var CurDate = new Date(valCurDate);
  9.  
  10.     if(FromDate > ToDate)
  11.     {
  12.         alert(fromname + " should be less than " + toname);
  13.         return false; 
  14.     }
  15.     else if(FromDate > CurDate)
  16.     {
  17.         alert("From date should be less than current date");
  18.         return false; 
  19.     }
  20.     else if(ToDate > CurDate)
  21.     {
  22.         alert("To date should be less than current date");
  23.         return false;
  24.     }

Last edited by acoder; July 3rd, 2009 at 02:57 PM. Reason: Added [code] tags
  #8  
Old July 3rd, 2009, 03:00 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: From Date & To Date Validation in javascript


Two things I should point out:
1. There's no need to create two objects for the current date. When you create a new Date object, it's automatically set to today's date:
Expand|Select|Wrap|Line Numbers
  1. var today = new Date();
2. You should validate the actual string for the "from" and "to" dates that they are valid dates, e.g. with regular expressions.

PS. please use code tags around your code: [code]like this[/code] It makes it a lot easier to read.
  #9  
Old July 3rd, 2009, 03:36 PM
Newbie
 
Join Date: Jun 2009
Posts: 29

re: From Date & To Date Validation in javascript


Thanks acoder for your feedback
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help gunimpi answers 0 January 10th, 2007 08:55 PM
Linking text boxes to buttons. Joel Barsotti answers 1 January 24th, 2006 06:25 AM
AutoPostBack & Validation Scott M. answers 4 November 18th, 2005 01:47 AM
"Split & Parts" different results in Firefox & IExplorer kinne answers 4 July 23rd, 2005 04:38 PM