JavaScript - comprobar fecha

 
Vista:

comprobar fecha

Publicado por Racsus (11 intervenciones) el 13/06/2003 13:45:15
Hola

necesito una función que me comprueba sin un campo es del tipo dd/mm/yyyy

gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:comprobar fecha

Publicado por palako (20 intervenciones) el 16/06/2003 21:07:59
Pues toma:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function isDate(dateStr)
{
 
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
    var matchArray = dateStr.match(datePat); // Formato correcto?
 
    if (matchArray == null)
    {
        alert("Formato de fecha incorrecto.");
        return false;
    }
 
    day = matchArray[1];
    month = matchArray[3];
    year = matchArray[5];
 
    if (month < 1 || month > 12)
    { // Comprueba el mes
        alert("El mes debe estar entre 1 y 12.");
        return false;
    }
 
    if (day < 1 || day > 31)
    {
        alert("El día debe estar entre 1 y 31.");
        return false;
    }
 
    if ((month==4 || month==6 || month==9 || month==11) && day==31)
    {
        alert("El mes "+month+" no tiene 31 días")
        return false;
    }
 
    if (month == 2)
    { // años bisiestos 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap))
        {
            alert("Febrero del " + year + " no tiene " + day + " dias");
            return false;
        }
    }
    return true; // fecha valida
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar