2
Hello, I am trying to validate birth date less than 15 years using the following rule:
1- If the user enters the date of birth, check if the day, month and year the user has typed, corresponds to less than 15 years, if yes, the button will be hidden.
Insert 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 now it is working validating only per year, I would like it to validate as follows:
the user was born 21/09/2000 however, his age is 14 years using this function above, tomorrow is day 22/09, the user will enter the data 22/09, the function will calculate 15 years, ok .
I would like help from you to calculate this way.
Thank you
We have a calculation problem when we consider the leap year... if making a difference by the exact time spent the day before the birthday can be considered as ok..
– h3nr1ke
have some example there ?
– hulckb
If so, please expedite your work :http://bit.ly/1efORk3 or http://bit.ly/1KqRCR8 this framework.
– Carlos Henrique
Thanks for the link and the tips ...
– hulckb