I would like to create a function to validate if a DOB date i.e: 10/05/2002 is allowed to register this year 2020, for example if user selects the three dropdowns 10 05 2020(today's date) it should be allowed to register and pass this validation because today turns 18.
Currently code is only validating using year value from Date object and selected year from dropdown.
function myFunction(Year) {//2020
var Year = 2020 > selected dropdown
var month = 10 > selected dropdown
var day = 05 > selected dropdown
//Today's day selected since today user turns 18 years old
var birthdate = month + '/' + day + '/' + Year;
var age = new Date().getFullYear() - new Date(birthdate).getFullYear();
//Day and Month Validation here
if (age <= 18) {
alert('Not Allowed To Register');
}
else {
alert('Allowed to register');
}
}
If dropdown option selections = From January 1st untill today(Oct 05 2020) it should be allowed to register Validation should be against Day and Month also not just the year to be 18 years old.
This is what I've tried after doing some research and different answers, but boolean value(dob_this_year) in calculateDOB() function still false and not validating correctly.
function MyFunction(year) {
var Year = 2020;
var month = 10;
var day = 05;
var birthdate = month + '/' + day + '/' + Year;
var isAllowedToRegister = calculateDOB(birthdate);
//var age = new Date().getFullYear() - new Date(birthdate).getFullYear();
if (!isAllowedToRegister) {
alert('Allowed to Register');
}
else {
alert('Not Allow to register yet!');
}
}
function calculateDOB(date) {
var now = new Date();
var current_year = now.getFullYear();
var year_diff = current_year - new Date(date).getFullYear();
var birthday_this_year = new Date(current_year, new Date(date).getMonth, new Date(date).getDate());
var dob_this_year = (now >= birthday_this_year);
return dob_this_year ? true : false;
}