I have an input of type text with a length = 2.
<input type="text" size="4" maxlength="2" name="dia" id="iddia"/>
Where I want to validate that what is written are only numbers and that if I write a number greater than or equal to 4, then I can not write more numbers because the day 45-66-81 does not exist, and if I write the first digit on 3, just allow me the values 30-31.
Why can I write any number from 1 to 99?
//Validar el dia.
function validacionDia(e){
var key = e.which || e.keyCode;
var value = document.getElementById("iddia").value;
var dia = value + String.fromCharCode(key);
if (isNaN(dia) === true || dia < 1 || dia > 31) {
return false;
}
return true;
}
miformulario.dia.addEventListener("change", function(){
return validacionDia(event);
}, false);
And for the validation of a year / birth? Accept values between 1960 and 2010
//Validar el año.
function validacionAnio(e){
var key = e.which || e.keyCode;
var value = document.getElementById("idanio").value;
var anio = value + String.fromCharCode(key);
return (anio>0 && anio<2010);
}