How can I validate that someone is over 18 from their date of birth?

I am doing validation for Driver’s Date of birth, it should be minimum of 18 from the current date.

var Dates = $get('<%=ui_txtDOB.ClientID %>');   
var Split = Dates.value.split("/");

if (parseInt(Split[2]) > 1993) 
{
    alert("DOB year should be less than 1993");
    Dates.focus();
    return false;
}

I am using this above JavaScript validation for checking a person’s DOB above 18, but it is not correct. I need to check with today’s date and it should be above 18. How can I compare and check with the current date?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think a better alternative would be to calculate the age of the user, and use that in your if statement.

See this SO answer on how to do just that:

Calculate age in JavaScript

Method 2

Try this.

var enteredValue = $get('<%=ui_txtDOB.ClientID %>');;
var enteredAge = getAge(enteredValue.value);
if( enteredAge > 18 ) {
    alert("DOB not valid");
    enteredValue.focus();
    return false;
}

Using this function.

function getAge(DOB) {
    var today = new Date();
    var birthDate = new Date(DOB);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }    
    return age;
}

Demo here: http://jsfiddle.net/codeandcloud/n33RJ/

Method 3

<script>


    function dobValidate(birth) {


        var today = new Date();
        var nowyear = today.getFullYear();
        var nowmonth = today.getMonth();
        var nowday = today.getDate();
        var b = document.getElementById('<%=TextBox2.ClientID%>').value;



        var birth = new Date(b);

        var birthyear = birth.getFullYear();
        var birthmonth = birth.getMonth();
        var birthday = birth.getDate();

        var age = nowyear - birthyear;
        var age_month = nowmonth - birthmonth;
        var age_day = nowday - birthday;


        if (age > 100) {
            alert("Age cannot be more than 100 Years.Please enter correct age")
            return false;
        }
        if (age_month < 0 || (age_month == 0 && age_day < 0)) {
            age = parseInt(age) - 1;


        }
        if ((age == 18 && age_month <= 0 && age_day <= 0) || age < 18) {
            alert("Age should be more than 18 years.Please enter a valid Date of Birth");
            return false;
        }
    }



</script>

Method 4

After looking at various methods of doing this, I decided the simplest way was to encode the dates as 8-digit integers. You can then subtract today’s code from the DOB code and check if it’s greater than or equal to 180000.

function isOverEighteen(year, month, day) {
  var now = parseInt(new Date().toISOString().slice(0, 10).replace(/-/g, ''));
  var dob = year * 10000 + month * 100 + day * 1; // Coerces strings to integers

  return now - dob > 180000;
}

Method 5

let TODAY = new Date(Date.now());
let EIGHTEEN_YEARS_BACK = new Date(new Date(TODAY).getDate() + "/" + new Date(TODAY).getMonth() + "/" + (new Date(TODAY).getFullYear() - 18));
let USER_INPUT = new Date("2003/12/13");
// Validate Now
let result = EIGHTEEN_YEARS_BACK > USER_INPUT // true if over 18, false if less than 18

I think this is the closest possible way to check.

Method 6

My approach is to find the date 18 (or any number) years ago from today, then see if that’s after (greater) than their dob. By setting all values to date objects it makes the comparison easy.

function is_of_age(dob, age) {
  // dates are all converted to date objects
  var my_dob = new Date(dob);
  var today = new Date();
  var max_dob = new Date(today.getFullYear() - age, today.getMonth(), today.getDate());
  return max_dob.getTime() > my_dob.getTime();
}

Because the Date object can parse strings in a variety of formats, You don’t have to worry too much about where dob is coming from. Simply call is_of_age(“1980/12/4”, 18); or is_of_age(“2005-04-17”, 13); or basically any string format or numeric that can be parsed as a Date parameter.

Method 7

My favourite approach is this one:

var dateOfBirth = new Date("02/23/1900");
// calculate difference between now and the dateOfBirth (in milliseconds)
var differenceMs = Date.now() - dateOfBirth.getTime();
// convert the calculated difference in date format
var dateFromEpoch = new Date(differenceMs); 
// extract year from dateFromEpoch
var yearFromEpoch = dateFromEpoch.getUTCFullYear();
// calculate the age of the user
var age = Math.abs(yearFromEpoch - 1970);

console.log("Age of the user: " + age + " years")

Method 8

You can use the below:

var birthDate = new Date("2018-06-21");
var birthDate1 = new Date("1975-06-21");
 function underAgeValidate(birthday) {
    const diff = Date.now() - birthday.getTime();
    const ageDate = new Date(diff);
    let age = Math.abs(ageDate.getUTCFullYear() - 1970);
      return age < 18;
  };

console.log(underAgeValidate(birthDate));
console.log(underAgeValidate(birthDate1));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x