Validar fecha

nano1710
26 de Noviembre del 2003
Tengo un textbox en el que quiero introducir una fecha con el formato dd/mm/aaaa
Podria alguien pasarme una funcion en javascript lo mas sencilla posible?

Gracias

Cristina
26 de Noviembre del 2003
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>var dia,mes,año,dia1,keyCode;
var axpr_reg =/^(d|[0][1-9]|[1][0-9]|[2][0123456789]|[3][01])$/;
var axpr_reg1 =/^(d|[0][1-9]|[1][012])$/;
var axpr_reg2 =/^([1][9][9][456789]|[2][0][012]d|[2][0][3][0])$/;
var SXIX=/^([9]d)$/;
var SXX=/^([0-5]d)$/;
var axpr_reg_mes=/^([0][1-9]|[1-9])$/
var axpr_reg_mes_trien=/^([2469]|[1][1]|[0][2469])$/
var axpr_reg_dia_trein=/^([3][1])$/
function fecha_compro(fecha_enviada,e){
if(window.event)keyCode=window.event.keyCode;
else if(e) keyCode=e.which;
if(keyCode==9){
if(document.getElementById(fecha_enviada.id).value=='')
document.getElementById(fecha_enviada.id).value='01/01/1994';
dia =document.getElementById(fecha_enviada.id).value.split("/")[0];
mes =document.getElementById(fecha_enviada.id).value.split("/")[1];
año =document.getElementById(fecha_enviada.id).value.split("/")[2];
if(SXIX.test(año))año=19+año;else if(SXX.test(año))año=20+año;else año=año
if (!(axpr_reg.test(dia) && axpr_reg1.test(mes) && axpr_reg2.test(año))){
alert("Introduzca de nuevo los datos.")
document.getElementById(fecha_enviada.id).value=""
document.getElementById(fecha_enviada.id).focus()}
else{
if((año%4!=0&&dia>=29&&mes==2)||(año%4==0&&dia>29&&mes==2)){
alert("Introduzca de nuevo los datos.")
document.getElementById(fecha_enviada.id).value=""
document.getElementById(fecha_enviada.id).focus()}
if((axpr_reg_mes_trien.test(mes)&&axpr_reg_dia_trein.test(dia))){
alert("Datos introducidos de manera incorrecta.")
document.getElementById(fecha_enviada.id).value=""
document.getElementById(fecha_enviada.id).focus()}
}
}
}
</script>
</head>

<body>
<input type="text" id="fecha_id" name="fecha_name" onKeyDown="fecha_compro(this,event)">
</body>
</html>