By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

From Date & To Date Validation in javascript

P: 31
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
Jul 3 '09 #1
Share this Question
Share on Google+
10 Replies


acoder
Expert Mod 15k+
P: 16,027
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.
Jul 3 '09 #2

P: 31
Thanks for the suggestion acoder it really helped me a lot
Jul 3 '09 #3

acoder
Expert Mod 15k+
P: 16,027
No problem. Perhaps you could post your working code for the benefit of others.
Jul 3 '09 #4

P: 31
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.                 }
Jul 3 '09 #5

P: 31
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
Jul 3 '09 #6

P: 31
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.     }
Jul 3 '09 #7

acoder
Expert Mod 15k+
P: 16,027
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.
Jul 3 '09 #8

P: 31
Thanks acoder for your feedback
Jul 3 '09 #9

P: 1
you can check this using the following jquery code

Expand|Select|Wrap|Line Numbers
  1.  var EnteredDate = document.getElementById("txtdate").value; //for javascript
  2.  
  3.             var EnteredDate = $("#txtdate").val(); // For JQuery
  4.  
  5.             var date = EnteredDate.substring(0, 2);
  6.             var month = EnteredDate.substring(3, 5);
  7.             var year = EnteredDate.substring(6, 10);
  8.  
  9.             var myDate = new Date(year, month - 1, date);
  10.  
  11.             var today = new Date();
  12.  
  13.             if (myDate > today) {
  14.                 alert("Entered date is greater than today's date ");
  15.             }
  16.             else {
  17.                 alert("Entered date is less than today's date ");
  18.             }
  19.         }
for more info refer this : check entered date is greater than current date
Aug 28 '13 #10

P: 1
Expand|Select|Wrap|Line Numbers
  1. if (iForm.DiddfromDate.value == "")
  2.       {
  3.         alert("  Please enter a value");
  4.         iForm.DiddfromDate.focus();
  5.         return false;
  6.       }
  7.          if (iForm.DiddtoDate.value == "")
  8.       {
  9.         alert("  Please enter a  value");
  10.         iForm.DiddtoDate.focus();
  11.         return false;
  12.       }
  13.             try {
  14.                 var d1 = iForm.DiddfromDate.value.substr(0, 2);
  15.                 var m1 = iForm.DiddfromDate.value.substr(3, 2);
  16.                 var y1 = iForm.DiddfromDate.value.substr(6, 4);
  17.                 var StrDate = m1 + "/" + d1 + "/" + y1;
  18.  
  19.                 var d2 = iForm.DiddtoDate.value.substr(0, 2);
  20.                 var m2 = iForm.DiddtoDate.value.substr(3, 2);
  21.                 var y2 = iForm.DiddtoDate.value.substr(6, 4);
  22.                 var EndDate = m2 + "/" + d2 + "/" + y2;
  23.  
  24.                 var startDate = new Date(StrDate);
  25.                 var endDate = new Date(EndDate);
  26.                 if (startDate > endDate) {
  27.                     alert('To date should be greater than From date.');
  28.                     iForm.DiddfromDate.value = '';
  29.                     iForm.DiddtoDate.value = '';
  30.                     iForm.DiddfromDate.focus();
  31.                     return false;
  32.                 }
  33.             } catch (e) { alert(e.Description); }
  34. return true;
  35. }
Mar 4 '14 #11

Post your reply

Sign in to post your reply or Sign up for a free account.