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

Issue

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?

Solution

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

Answered By – Curtis

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *