JavaScript - Campo de Formulario para Horas con dos puntos automático al ir escribiendo

 
Vista:
Imágen de perfil de Eduardo Arroyo
Val: 159
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Campo de Formulario para Horas con dos puntos automático al ir escribiendo

Publicado por Eduardo Arroyo (176 intervenciones) el 03/01/2020 04:54:12
Hola a Todos quiero usar un campo de formulario sin el complique del usar el time en html5 lo que deseo es que en el campo yo al escribir por ejemplo 500 aparezca así 5:00 es decir ese dos puntos : salga automático al ir escribiendo en el campo y separe esa hora y que este sea solo numérico.

Mil 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
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Campo de Formulario para Horas con dos puntos automático al ir escribiendo

Publicado por ScriptShow (692 intervenciones) el 04/01/2020 23:28:33
Saludos Eduardo,

hay varias formas posibles. Veamos un ejemplo sencillo, adaptable, compatible, etc...

1
2
3
4
5
6
7
8
9
10
11
12
<button onclick="test()">Test</button>
 
<input type="text" id="demo">
 
<script>
function test() {
var str = document.getElementById("demo").value;
if(isNaN(str))return;
document.getElementById("demo").value =
str.slice(0,1) + ":" + str.substr(1,4);
}
</script>

Espero sea útil.
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
Imágen de perfil de Alejandro
Val: 1.448
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Campo de Formulario para Horas con dos puntos automático al ir escribiendo

Publicado por Alejandro (532 intervenciones) el 30/01/2020 15:53:32
  • Alejandro se encuentra ahora conectado en el
  • chat de PHP
Hice esto, seguro tiene alguna falla pero bien te sirve como base.
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
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);
 
			if ( (textStart+textEnd).search(':')!=-1 && e.key==':' ) return;
			var textNew = textStart + e.key + textEnd;
			textNew = textNew.replace(':','');
			if ( textNew.length>2 ){
				textStart = textNew.substr(0,textNew.length-2);
				textEnd   = textNew.substr(-2);
				textNew = textStart + ':' + textEnd;
				position++;
			} else if ( textNew>59 ) {
				textStart = textNew.substr(0,1);
				textEnd   = textNew.substr(1);
				textNew = textStart + ':' + textEnd;
				position++;
			}
 
			var textPart=textNew.split(':');
 
			if ( textPart.length == 2 && textPart[0]>23 || textPart[1]>59 ) return;
 
			this.value = textNew;
 
			this.selectionStart = position+1;
			this.selectionEnd = position+1;
		}
	}
 
	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 {
				textPart.push(textPart[0].padStart(2,'0'));
				textPart[0]='00';
			}
			this.value=textPart.join(':');
		}
	}
</script>
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