Solved

Java Script date validator

Posted on 2010-11-29
5
834 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
push Book Today button to right 10 36
What does GoogleTagMgr javascripts below do 5 34
Change to file doesn't show up 16 52
Google Maps with Webforms 1 12
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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

856 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