Código de JavaScript - Validar hora mientras se va escribiendo.

0.1
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 11 de Marzo del 2020gráfica de visualizaciones de la versión: 0.1
1.114 visualizaciones desde el 11 de Marzo del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
Hora: <input type="text" class="hora" size="5" autofocus />
<script>
	document.querySelector('.hora').addEventListener('keypress',validateHour);
	document.querySelector('.hora').addEventListener('blur',formatHour);
 
	function validateHour( e )
	{
		e.preventDefault( );
		if ( !isNaN(e.key) || e.key==':' )
		{
			var position  = this.selectionStart;
			var text      = this.value;
			var textStart = text.substr(0,position);
			var textEnd   = text.substr(this.selectionEnd);
			var textNew;
 
			if ( e.key != ':' || ( e.key == ':' && (textStart+textEnd).search(':') == -1 ) )
			{
				textNew = textStart + e.key + textEnd;
 
				if ( textNew.search(':') != -1 )
				{
					var aText = textNew.split(':');
					textStart = aText[0];
					textEnd   = aText[1];
					if( textStart<24 && textEnd<60 )
					{
						if ( e.key == ':' && textStart.length < 2 )
						{
							if ( textStart.length == 0 )
							{
								position++;
							}
							textStart = textStart.padStart(2, '0');
							position++;
						}
						textNew = textStart + ':' + textEnd;
					}
					else
					{
						return;
					}
				}
				else
				{
					switch( textNew.length )
					{
						case 1:
							break;
 
						case 2:
							if ( textNew >= 24 )
							{
								textNew = '00:' + textNew;
								position = 4;
							}
							else if ( e.key == ':' )
							{
								textNew = textNew + ':';
								position++;
							}
							break;
 
						case 3:
							if ( textNew.substr(0, 2) < 24 && textNew.substr(2, 1) < 60 )
							{
								textNew = textNew.substr(0, 2) + ':' + textNew.substr(2, 1);
							}
							else if ( textNew.substr(0, 1) < 24 && textNew.substr(1, 2) < 60 )
							{
								textNew = textNew.substr(0, 1).padStart(2, '0') + ':' + textNew.substr(2, 1);
							}
							else
							{
								return;
							}
							position++;
							break;
 
						case 4:
							if ( textNew.substr(0, 2) < 24 && textNew.substr(2, 2) < 60 )
							{
								textNew = textNew.substr(0, 2) + ':' + textNew.substr(2, 2);
								position++;
							}
							else
							{
								return;
							}
							break;
 
						default:
							return false;
 
					}
				}
				if ( textNew.length <= 5 )
				{
					this.value = textNew;
 
					this.selectionStart = ++position;
					this.selectionEnd   = this.selectionStart;
				}
			}
		}
	}
 
 
 
	function formatHour()
	{
		var text = this.value;
		if ( text != "" )
		{
			textPart = text.split(':');
 
			if ( textPart.length == 2 )
			{
				textPart[0] = textPart[0].padStart(2, '0');
				textPart[1] = textPart[1].padStart(2, '0');
			}
			else
			{
				switch ( text.length )
				{
					case 1:
					case 2:
						textPart[0] = 0;
						textPart.push(text);
						break;
 
					case 3:
						if ( text.substr(0, 1)<24 && text.substr(1, 2)<60 )
						{
							textPart[0] = text.substr(0, 1);
							textPart.push(text.substr(1, 2));
						}
						else if ( text.substr(0, 2)<24 && text.substr(2, 1)<60 )
						{
							textPart[0] = text.substr(0, 2);
							textPart.push(text.substr(2, 1));
						}
						break;
 
					case 4:
						textPart[0] = text.substr(0, 2);
						textPart.push(text.substr(2, 2));
						break;
				}
			}
			if ( textPart.length == 2 )
			{
				textPart[0] = String(textPart[0]).padStart(2, '0');
				textPart[1] = String(textPart[1]).padStart(2, '0');
				this.value = textPart.join(':');
			}
			else
			{
				this.value = '';
			}
		}
	}
</script>



Comentarios sobre la versión: 0.1 (2)

Imágen de perfil
11 de Marzo del 2020
estrellaestrellaestrellaestrellaestrella
si funcionó pero hay un problema muchas personas no están acostumbradas a ingresar una hora usando el cero al principio y si esta se ingresa por ejemplo 2:30 no deja el formulario, hay que agregarla 02:30 y las personas prefieren agregarlo sin el cero y que el sistema les ponga el cero automático. solo falta es eso...
Responder
Imágen de perfil
14 de Marzo del 2020
estrellaestrellaestrellaestrellaestrella
Saludos,

una opción adaptable al script:

1
2
3
4
if ( textNew < 10 )
{
textNew = '0' + textNew;
}

P.D.: No he revisado detenidamente, ni probado el código.

Espero sea útil.
Responder

Comentar la versión: 0.1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6015