Solved

Java Script date validator

Posted on 2010-11-29
5
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

733 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