Hello, I am trying to validate birthdate less than 15 years using the following rule:
1- If the user enters the date of birth, check if the day, month and year that the user entered, is less than 15 years, if so, the button will be hidden.
Enter the following function below:
function calculaIdade(dobString) {
var data_nasc = document.getElementById('data_cri').value.split("/");
var verifica = data_nasc[2]+data_nasc[1]+data_nasc[0];
var dob = new Date(dobString);
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var birthdayThisYear = new Date(currentYear, dob.getMonth(), dob.getDate());
var verifica = currentYear - dob.getFullYear();
alert(verifica); //mostra a idade
alert(currentYear); // mostra o ano
alert(birthdayThisYear);
if (verifica >= 15 && currentYear < birthdayThisYear ){
//alert('pode');
document.getElementById('mostravid').style.display = "block";
} else {
//alert('nao pode');
document.getElementById('mostravid').style.display = "none";
}
if (verifica == ''){
document.getElementById('mostravid').style.display = "none";
}
}
<input type='text' name='data_cri' id='data_cri' value='<?=$data_cri?>' size='12' maxlength='10' onkeyup='formataData(this,this.value);' onblur='return calculaIdade(this.value)'>
For the time being it works by only validating for a year, I would like it to validate as follows:
The user was born 9/21/2000 but his age is 14 years using this function above, tomorrow is 22/09, the user will enter the data 22/09, the function will calculate 15 years, ok .
I'd like to help you figure it out.
Thank you