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?

2
  • Wouldn't searching SO with [javascript] age validation provide some answers? Commented Sep 3, 2012 at 13:16
  • possible duplicate of Calculate age in JavaScript Commented Sep 3, 2012 at 17:41

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.