Solved

Java Script date validator

Posted on 2010-11-29
5
828 Views
Last Modified: 2012-05-10
How can I validate the date (via a popcalendar) to were the end date can not be more than 6 months (180 days) greater than the start date?
Code:
function datePickerClosed(dateField) {
            var dateObj = getFieldDate(dateField.value);
            var today = new Date();
            today = new Date(today.getFullYear(), today.getMonth(), today.getDate());

            if (dateField.name == "TextBox1") {
                if (dateObj < today) {
                    // if the date is before today, alert the user and display the datepicker again
                    alert("Please enter a date that is today or later");
                   dateField.value = "";
                }
            }


            if (dateField.name == "TextBox2") {
                if (dateObj > 180) {
                    alert("Please enter in a date that is no more than 6 months (180 days) from the start date");
                    dateField.value = "";
                }
            }
        }
0
Comment
Question by:newjeep19
5 Comments
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34231149
use this to get the date 180 days from the selected date
  var myDate = new Date();
   myDate.setDate(dateObj.getDate()+180);
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34232674
date difference is what you are actually looking at
go through the following link, it will help:-

http://javascript.internet.com/math-related/date-difference.html

this one gives the js file with datediff function
http://www.scripts.com/viewscript/datediff-and-dateadd-functions-now-in-javascript/21453/

hope this helps
:-)
0
 

Author Comment

by:newjeep19
ID: 34232880
No I want the end date to be no greater than 180 day's (6 months ) from the start date. So, If the endate is 7 month's past the start date then the alert will state: you selected a date that is more than six months form the start date and then clears the end date textbox and the user has to select another date (via popcalender) that is not greater than the start date.
0
 
LVL 14

Expert Comment

by:robasta
ID: 34237393
  I have removed some of your code for clarity

    var startDate = "put start date here";
    var endDate = "put end date here";

    var maxEndDate = startDate.getDate()+180; //calculate the MaximumPossible End date, this is the date we use to compare to the EndDate. >>>The EndDate should not be more than this max limit

    if (endDate > maxEndDate) {
                    alert("Please enter in a date that is no more than 6 months (180 days) from the start date");
                    dateField.value = "";
     }
           
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 34238135
If you try to download datediff.js file from the links given in my previous post, you will easily be able to play around with the dates. Please try and let me know the issues if any.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question