ReynierPM
ReynierPM

Reputation: 18660

Validates if a person is older than 18 or have 18 based on birthday

I'm writing a little code to check if a person have 18 years or is older given her/his birthday. This is what I have done til now:

function validateAge(birthday, minage, separator) {
    if (!parseInt(minage)) {
        return false;
    }

    var $split = birthday.split(separator);

    var $dateFullYear = $split[2];
    var $dateMonth = $split[1];
    var $dateDay = $split[0];

    var $tempDate = new Date($dateFullYear + minage, $dateMonth, $dateDay);

    if ($tempDate <= new Date()) {
        return true;
    } else {
        return false;
    }

    return false;

}

But I give always a valid response when for example if I call the function with this parameters: validateAge('09/09/2014', 18, '/') it should return false since it's today. I'm making a mistake somewhere but I don't see it, can any help me?

Upvotes: 2

Views: 862

Answers (5)

Michael Martin-Smucker
Michael Martin-Smucker

Reputation: 12705

The month needs to be zero-based.

new Date(2014, 9, 9) // Thu Oct 09 2014 00:00:00 GMT-0600 (MDT)

You would expect the month 9 to be September, but it's actually October because January is 0. This is throwing off your comparison.

Upvotes: 1

kol
kol

Reputation: 28688

Let's see an example:

validateAge('09/09/1990', 18, '/')

In this case, your code will do this:

var $tempDate = new Date('1990' + 18, '09', '09');

which is the same as

var $tempDate = new Date(199018, 9, 9);

This problem can be solved by parsing the parts of birthday:

var $dateFullYear = parseInt($split[2]);
var $dateMonth = parseInt($split[1]) - 1; // months are zero based
var $dateDay = parseInt($split[0]);

Upvotes: 1

chridam
chridam

Reputation: 103355

You could try rewrite your function as follows, where the month is zero-based:

function validateAge(birthday, minage, separator) {
    if (!parseInt(minage)) {
        return false;
    }
    var split = birthday.split(separator);
    var dateFullYear = split[2];
    var dateMonth = split[1];
    var dateDay = split[0];
    var mydate = new Date();

    mydate.setFullYear(dateFullYear, dateMonth-1, dateDay);

    var currdate = new Date();
    var setDate = new Date();         
    setDate.setFullYear(mydate.getFullYear() + minage, dateMonth-1, dateDay);

    return (currdate - setDate) > 0;

}

var valid = validateAge('09/09/2014', 18, '/');
console.log(valid); // false

JSFiddle

Upvotes: 0

Andy
Andy

Reputation: 63524

You need to wrap your $dateFullYear in a parseInt - its trying to add a string and an integer together and giving you the wrong date.

var $tempDate = new Date(parseInt($dateFullYear, 10) + minage, $dateMonth, $dateDay);

Upvotes: 0

mccannf
mccannf

Reputation: 16659

You should parse the split statements like so and subtract one from the month (zero-based):

var $dateFullYear = parseInt($split[2],10);
var $dateMonth = parseInt($split[1],10)-1;
var $dateDay = parseInt($split[0],10);

Upvotes: 3

Related Questions