JavaScript - Validar hora

 
Vista:

Validar hora

Publicado por Mercedes (9 intervenciones) el 09/01/2008 11:03:19
Buenos días a todos y Feliz Año!!

Quisiera saber si alguien tiene algún script para validar la hora en formato hh:mm:ss. No pretendo que me hagan el script, sino reutilizar alguno existente.

Muchas gracias

Un saludo
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:Validar hora

Publicado por Laura (6 intervenciones) el 09/01/2008 13:23:47
Hola, feliz año!!

Pues yo hace un ratito he encontrado este script que no está mal:
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
<script language="javascript" type="text⁄javascript">
 
function CheckTime(str)
{
	hora=str.value;
	if (hora=='') {
		return;
	}
	if (hora.length>8) {
		alert("Introdujo una cadena mayor a 8 caracteres");
		return;
	}
	if (hora.length!=8) {
		alert("Introducir HH:MM:SS");
		return;
	}
	a=hora.charAt(0); //<=2
	b=hora.charAt(1); //<4
	c=hora.charAt(2); //:
	d=hora.charAt(3); //<=5
	e=hora.charAt(5); //:
	f=hora.charAt(6); //<=5
	if ((a==2 && b>3) || (a>2)) {
		alert("El valor que introdujo en la Hora no corresponde, introduzca un digito entre 00 y 23");
		return;
	}
	if (d>5) {
		alert("El valor que introdujo en los minutos no corresponde, introduzca un digito entre 00 y 59");
		return;
	}
	if (f>5) {
		alert("El valor que introdujo en los segundos no corresponde");
		return;
	}
	if (c!=':' || e!=':') {
		alert("Introduzca el caracter ':' para separar la hora, los minutos y los segundos");
		return;
	}
}
 
</script>

Luego en el campo del formulario donde haya que introducir la hora pones como atributo: onBlur="CheckTime(this)", y así te hace la comprobación en cuanto pinchas fuera del campo.

Espero que te ayude ;-)
Saludos!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

RE:Validar hora

Publicado por Roger (1 intervención) el 28/12/2019 00:20:59
Muchas gracias, me ha servido mucho !!
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

RE:Validar hora

Publicado por celeste (2 intervenciones) el 26/07/2008 08:32:54
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
46
47
48
49
<SCRIPT >
function IsValidTime(timeStrtotal) {
 
timeStr = timeStrtotal.value;
var timePat = /^(d{1,2}):(d{2})(:(d{2}))?(s?())?$/;
 
var matchArray = timeStr.match(timePat);
 
if (timeStrtotal.value == "") { return true };
if (matchArray == null) {
   alert("No es un formato valido [HH:MM:SS]");
   timeStrtotal.value="";
   timeStrtotal.focus();
    return false;
}
 
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];
 
if (second=="") { second = null; }
if (ampm=="") { ampm = null }
 
if (hour < 0  || hour > 23) {
alert("La hora debe estar entre 0 y 23 para formato militar");
   timeStrtotal.focus();
return false;
}
if  (hour > 12 && ampm != null) {
alert("No puedes especificar AM o PM para el formato militar.");
   timeStrtotal.focus();
return false;
}
 
if (minute<0 || minute > 59) {
alert ("Minutos deben estar entre 0 y 59.");
   timeStrtotal.focus();
return false;
}
 
if (second != null && (second < 0 || second > 59)) {
alert ("Segundos deben estar entre 0 y 59.");
   timeStrtotal.focus();
return false;
}
return true;
}
</Scrip>
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

RE:Validar hora

Publicado por celeste (2 intervenciones) el 26/07/2008 08:32:58
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
46
47
48
49
<SCRIPT >
function IsValidTime(timeStrtotal) {
 
timeStr = timeStrtotal.value;
var timePat = /^(d{1,2}):(d{2})(:(d{2}))?(s?())?$/;
 
var matchArray = timeStr.match(timePat);
 
if (timeStrtotal.value == "") { return true };
if (matchArray == null) {
   alert("No es un formato valido [HH:MM:SS]");
   timeStrtotal.value="";
   timeStrtotal.focus();
    return false;
}
 
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];
 
if (second=="") { second = null; }
if (ampm=="") { ampm = null }
 
if (hour < 0  || hour > 23) {
alert("La hora debe estar entre 0 y 23 para formato militar");
   timeStrtotal.focus();
return false;
}
if  (hour > 12 && ampm != null) {
alert("No puedes especificar AM o PM para el formato militar.");
   timeStrtotal.focus();
return false;
}
 
if (minute<0 || minute > 59) {
alert ("Minutos deben estar entre 0 y 59.");
   timeStrtotal.focus();
return false;
}
 
if (second != null && (second < 0 || second > 59)) {
alert ("Segundos deben estar entre 0 y 59.");
   timeStrtotal.focus();
return false;
}
return true;
}
</Scrip>
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

RE:Validar hora

Publicado por Javier (1 intervención) el 12/08/2008 22:01:34
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
<script language="JavaScript">
 
function FormatoHora(evt,str)
{
var nav4 = window.Event ? true : false;
var key = nav4 ? evt.which : evt.keyCode;
hora=str.value
 
if(hora.length==0)
{
	return ((key >= 48 && key <= 50));
}
if(hora.length==1)
{
	if(hora.charAt(0)==2)
	{return ((key >= 48 && key <= 51));	}
	else{return ((key >= 48 && key <= 57));}
}
if(hora.length==2)
{
	return ((key == 58));
}
if(hora.length==3)
{
	return ((key >= 48 && key <= 53));
}
if(hora.length==4)
{
	return ((key >= 48 && key <= 57));
}
if(hora.length>4)
{
	return false;
}
}
 
</script>

en el html debes poner

1
<input type="text" name="entrada"  onKeyPress="return FormatoHora(event,this)">
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