Validate date for anyone over 18 with jQuery

I have a form on my site that should validate for anyone who is over 18.

var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
var output = currdate - mydate
if ((currdate - mydate) > 0){
    // you are not 18
}

But it working totally opposite way. I would like the if statement to take action when user is over under 18 years old.

Thank you for your help in advance

Answers:

Answer

check this DEMO

var day = 12;
var month = 12;
var year = 2006;
var age = 18;
var setDate = new Date(year + age, month - 1, day);
var currdate = new Date();

if (currdate >= setDate) {
  // you are above 18
  alert("above 18");
} else {
  alert("below 18");
}

Answer
var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age =  18;

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

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);

if(currdate < mydate)
{
    alert('You must be at least 18 years of age.');
}
Answer

Here is a somewhat lighter version that I tested:

var day = 1;
var month = 1;
var year = 1999;
var age = 18;

var cutOffDate = new Date(year + age, month, day);

if (cutOffDate > Date.now()) {
    $('output').val("Get Outta Here!");
} else {
    $('output').val("Works for me!");
}

The key is to add the minimum age to the birthdate and confirm that it is before the current date. You are checking if the current date minus the minimum age (basically the latest birthdate allowed) was greater than than the birthdate provided, which will give you the reverse.

Answer

18 year old validation rule for jQuery Validator plugin using addMethod function.

jQuery.validator.addMethod(
        "validDOB",
        function(value, element) {              
            var from = value.split(" "); // DD MM YYYY
            // var from = value.split("/"); // DD/MM/YYYY

            var day = from[0];
            var month = from[1];
            var year = from[2];
            var age = 18;

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

            var currdate = new Date();
            var setDate = new Date();

            setDate.setFullYear(mydate.getFullYear() + age, month-1, day);

            if ((currdate - setDate) > 0){
                return true;
            }else{
                return false;
            }
        },
        "Sorry, you must be 18 years of age to apply"
    );

and

$('#myForm')
        .validate({
            rules : {
                myDOB : {
                    validDOB : true
                }
            }
        });
Answer

if it's working the opposite way have you tried swapping the > for a < on the second to last line?

Answer

I think it will be easier to understand if we rename the variables

mydate => givenDate
currdate => thresholdDate

if givenDate > thresholdDate => you are not 18
else => you are 18

i.e.

if ( givenDate > thresholdDate ){
    // you are not 18
}

i.e

if ((givenDate - thresholdDate) > 0){
    // you are not 18
}

i.e.

if ((mydate - currdate ) > 0){
    // you are not 18
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.