473,379 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

From Date & To Date Validation in javascript

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
10 65631
acoder
16,027 Expert Mod 8TB
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
Thanks for the suggestion acoder it really helped me a lot
Jul 3 '09 #3
acoder
16,027 Expert Mod 8TB
No problem. Perhaps you could post your working code for the benefit of others.
Jul 3 '09 #4
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
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
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
16,027 Expert Mod 8TB
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
Thanks acoder for your feedback
Jul 3 '09 #9
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
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

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

Similar topics

6
by: Piotr Pietrowski | last post by:
Hello everybody, I have a *big* problem. I thought its not that big problem for you professionals... Anyway, I have a begin date which has 3 dropdown boxes (day/Month/Year). The same for the...
1
by: LL | last post by:
Hi, How to get the current Date & Time? Date() & Time() ? Thanks.
3
by: mark4asp | last post by:
How can I stop my Calendar control from firing the form validation events? I have a form containing several controls which have several validation controls each. One control is a TextBox...
3
by: Bob Sanderson | last post by:
I have a PHP web page which uses a HTML form. I would like to enter dates into the date fields using a JavaScript calendar, similar to the way phpMyAdmin does. Can anyone recommend a JavaScript...
1
by: adeebraza | last post by:
Hi, Every Body Following is code for Showing Actual Date & Time on the form and also record Date & Time of an event. See the following and use Call Modified when you want to record an event in...
10
by: shakefu | last post by:
I'm new to python and I was wondering if there are any intelligent date/time parsing modules out there. I've looked at strptime (or whichever it is) and mxDateTime from the eGenix package. I need...
1
Stang02GT
by: Stang02GT | last post by:
Here is the issue that I am having. I have two text feilds where users need to enter a "From Date:" and a "To Date:" they then hit a update button and my code will pull back the data for the date...
3
by: Jared Elyea | last post by:
I had no experience in VB before writing this so bare with me... the function below seems to work for certain cases, but not all (in some cases it is a week behind). Can anybody help me figure this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.