473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

From Date & To Date Validation in javascript

31 New Member
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 65662
acoder
16,027 Recognized Expert Moderator MVP
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
kirankumarn
31 New Member
Thanks for the suggestion acoder it really helped me a lot
Jul 3 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
No problem. Perhaps you could post your working code for the benefit of others.
Jul 3 '09 #4
kirankumarn
31 New Member
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
kirankumarn
31 New Member
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
kirankumarn
31 New Member
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 Recognized Expert Moderator MVP
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
kirankumarn
31 New Member
Thanks acoder for your feedback
Jul 3 '09 #9
codingsolver
1 New Member
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
luckylak
1 New Member
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
2203
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
1634
by: LL | last post by:
Hi, How to get the current Date & Time? Date() & Time() ? Thanks.
3
2162
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
4481
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
1833
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
4450
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
1541
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
2717
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...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.