Solved

Java Script date validator

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

623 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