Skip to content
Advertisement

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement