JavaScript: Age Validator

Published on Author craigzearfossLeave a comment
function isOfAge(year, month, day, age) {
    age = age || 18;

    var mydate = new Date();
    mydate.setFullYear(year, month-1, day);

    var currdate = new Date();
    currdate.setFullYear(currdate.getFullYear() - age);
    if ((currdate - mydate) < 0) {
        return false;
    }
    return true;
}

References:

  • Validate Age | CSS-Tricks

Leave a Reply

Your email address will not be published. Required fields are marked *