Solved

Java Script date validator

Posted on 2010-11-29
5
818 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
  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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now