I'm using this javascript function that calculates age:
<script type = "text/javascript" >
function calcularIdade(aniversario) {
var nascimento = aniversario.split("/");
var dataNascimento = new Date(parseInt(nascimento[2], 10),
parseInt(nascimento[1], 10) - 1,
parseInt(nascimento[0], 10));
var diferenca = Date.now() - dataNascimento.getTime();
var idade = new Date(diferenca);
return Math.abs(idade.getUTCFullYear() - 1970);
}
</script>
This is the line where I make the calculation, remembering that the mask of this field is "29/11/2017"
<input type="text" id="nascimento" onkeypress="mascara(this,mdata)" maxlength="10" name="nascimento" onblur="calcularIdade(this)" runat="server" class="form-control" />
When I am typing the date, it is not reporting the age, I checked it and it is returning the following error:
Uncaught TypeError: anniversary.split is not a function to calculateIty (Persons.aspx? id = 2: 444) at HTMLInputElement.onblur
I get error on this line:
var nascimento = aniversario.split("/");
I've tried in many ways, checked the value, it's coming out in the format dd / mm / yyyy
Updating: Function that I use to put mask in the field:
function mascara(o, f) {
v_obj = o
v_fun = f
setTimeout("execmascara()", 1)
}
function execmascara() {
v_obj.value = v_fun(v_obj.value)
}
function mdata(v) { // máscara para o user digitar sempre dd/mm/aaaa
v = v.replace(/\D/g, ""); //Remove tudo o que não é dígito
v = v.replace(/(\d{2})(\d)/, "$1/$2");
v = v.replace(/(\d{2})(\d)/, "$1/$2");
v = v.replace(/(\d{2})(\d{2})$/, "$1$2");
return v;
}