Connecting Tech Pros Worldwide Forums | Help | Site Map

From Date & To Date Validation in javascript

Newbie
 
Join Date: Jun 2009
Posts: 31
#1: Jul 3 '09
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

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 3 '09

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.
Newbie
 
Join Date: Jun 2009
Posts: 31
#3: Jul 3 '09

re: From Date & To Date Validation in javascript


Thanks for the suggestion acoder it really helped me a lot
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jul 3 '09

re: From Date & To Date Validation in javascript


No problem. Perhaps you could post your working code for the benefit of others.
Newbie
 
Join Date: Jun 2009
Posts: 31
#5: Jul 3 '09

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.                 }
Newbie
 
Join Date: Jun 2009
Posts: 31
#6: Jul 3 '09

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
Newbie
 
Join Date: Jun 2009
Posts: 31
#7: Jul 3 '09

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.     }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Jul 3 '09

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.
Newbie
 
Join Date: Jun 2009
Posts: 31
#9: Jul 3 '09

re: From Date & To Date Validation in javascript


Thanks acoder for your feedback
Reply