2

I will get the date of birth value dynamically from users visiting to the site, but when the user will insert birthday value I have to check whether the user is 18 years old or not.

user will insert a value like 18/09/2012 as dd/mm/yyyy format.

    var arr   = date.split("/"); //date is the value of birth date.
    var day   = arr[0];
    var month = arr[1];
    var year  = arr[2]; 

How should I do it?

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
user1638279
  • 523
  • 2
  • 12
  • 26
  • Wouldn't searching SO with `[javascript] age validation` provide some answers? – KooiInc Sep 03 '12 at 13:16
  • possible duplicate of [Calculate age in JavaScript](http://stackoverflow.com/questions/4060004/calculate-age-in-javascript) – jscs Sep 03 '12 at 17:41

2 Answers2

1

Take a look at the following question, it will help you calculate the age -
Calculate age in JavaScript

function getAge(dateString) 
{
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) 
    {
        age--;
    }
    return age;
}
Community
  • 1
  • 1
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
0

you may try any of these functions Age Calculation from Date of Birth Using Javascript/Jquery or check this one Calculate age in JavaScript

Community
  • 1
  • 1
NoNaMe
  • 6,020
  • 30
  • 82
  • 110