1

I need to validate different date's with some javascript(jquery).

I have a textbox with, the inputmask from jquery (http://plugins.jquery.com/plugin-tags/inputmask). The mask that i use is "d/m/y".

Now i have set up a CustomValidator function to validate the date.

I need 2 functions. One to check if the given date is greater then 18 years ago. You must be older then 18 year. One function to check if the date is not in the future. It can only in the past.

The function are like

function OlderThen18(source, args) {
}

function DateInThePast(source, args) {
}

As you know the value you get back with args.Value is 27/12/1987 .

But how can i check this date in the functions? So that i can set args.IsValid to True or False.

I tried to parse the string(27/12/1987) that i get back from the masked textbox to a date but i get always a value back like 27/12/1988. So how could I check the given dates with the other dates?

1
  • Is there a reason that you aren't using some sort of date picker plugin? Commented Apr 27, 2011 at 0:59

4 Answers 4

1

The simple way is to add 18 years to the supplied date and see if the result is today or earlier, e.g.:

// Input date as d/m/y or date object
// Return true/false if d is 18 years or more ago
function isOver18(d) {
  var t;
  var now = new Date();
  // Set hours, mins, secs to zero
  now.setHours(0,0,0);

  // Deal with string input
  if (typeof d == 'string') {
    t = d.split('/');
    d = new Date(t[2] + '/' + t[1] + '/' + t[0]);
  }

  // Add 18 years to date, check if on or before today
  if (d.setYear && d.getFullYear) {
    d.setYear(d.getFullYear() + 18);
  }
  return d <= now;
}

// For 27/4/2011
isOver18('27/4/2011'); // true
isOver18('26/4/2011'); // true
isOver18('28/4/2011'); // false
Sign up to request clarification or add additional context in comments.

Comments

1

try this to start:

var d = new Date(myDate);
var now = new Date();
if ((now.getFullYear() - d.getFullYear()) < 18) {
    //do stuff
}

2 Comments

Do not use getYear() unless you deal with browser quirks. Use getFullYear(). Also, simply subtracting the year will not accurately determine if someone is 18 or not, e.g. someone born on 1993/12/31 is not yet 18, but 2011 - 1993 = 18.
@rob - i agree with the getFullYear() but i was just presenting a general solution for the OP. I'm sure he can figure out the math on his own :)
0

The javascript date object is quite flexible and can handle many date strings. You can compare two Date objects or use the Date interface methods, such as getSeconds() of getFullYear() in order to deduce useful data regarding the date.

See Date object reference formore details.

Comments

0

You'll need to construct, modify and compare Date objects - something like this:

// str should already be in dd/mm/yyyy format
function parseDate(str) {
    var a = str.split('/');
    return new Date(parseInt(a[2], 10), // year
                    parseInt(a[1], 10) - 1, // month, should be 0-11
                    parseInt(a[0], 10)); // day
}

// returns a date object for today (at midnight)
function today() {
    var date = new Date();
    date.setHours(0, 0, 0);
    return date;
}

function DateInThePast(str) {
    // date objects can be compared like numbers
    // for equality (==) you'll need to compare the value of date.getTime()
    return parseDate(str) < today();
}

function OlderThan18(str) {
    // left as an exercise for the reader :-)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.