JavaScript - Validar varios input text con for()

 
Vista:
Imágen de perfil de Victor
Val: 4
Ha disminuido su posición en 10 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Validar varios input text con for()

Publicado por Victor (2 intervenciones) el 05/06/2018 05:49:48
Hola.

Soy novato así que perdón por si incluyo burradas en el código. Quiero validar con un for tres entradas de input tipo text. Para ello he probado a hacerlo como en el código de abajo. En consola del navegador me aparece que el document.getElementbyId da null... si alguien me puede dar alguna pista de qué hago mal se agradece.

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
<!DOCTYPE html>
 
<html>
	<head>
		<title>Testing 01</title>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	</head>
	<body>
		<form action='' onsubmit="validar()">
			<input type="text" id="number1" size="3" maxlength="3">
			<input type="text" id="number2" size="3" maxlength="3">
			<input type="text" id="number3" size="3" maxlength="3">
 
			<input type="submit" id="boton"  >
		</form>
		<script type="text/javascript">
 
 
			function validar(){
				for (i=1 ; i<=3 ; i++ ){
				if  ((isNaN(document.getElementById('number'+i).value)) )
				{
					document.write("El valor  de la entrada"+ i +"  no es un número");
 
				}
				else  {
					document.write("Valor de la entrada "+ i +"  correcto");
 
				}
			}
		}
 
			</script>
 
	</body>
</html>
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 xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Validar varios input text con for()

Publicado por xve (2100 intervenciones) el 05/06/2018 13:21:27
Hola victor, si haces un document.write(), elimina el contenido del documento, por lo que te dara error en la segunda iteración del bucle... para ello, prueba a enviarlo a la consola o a un elemento... aquí te he modificado el código para que no se envie el formulario y puedas ver los mensajes en la consola.

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
<!DOCTYPE html>
 
<html>
	<head>
		<title>Testing 01</title>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	</head>
	<body>
		<form action='' onsubmit="return validar()">
			<input type="text" id="number1" size="3" maxlength="3">
			<input type="text" id="number2" size="3" maxlength="3">
			<input type="text" id="number3" size="3" maxlength="3">
 
			<input type="submit" id="boton"  >
		</form>
		<script type="text/javascript">
 
 
        function validar(){
            for (i=1 ; i<=3 ; i++ ){
                if (isNaN(parseInt(document.getElementById('number'+i).value)))
                {
                    console.log("El valor de la entrada"+ i +" no es un número");
                }
                else  {
                    console.log("Valor de la entrada "+ i +" correcto");
                }
            }
            return false;
		}
 
		</script>
 
	</body>
</html>
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
Imágen de perfil de Victor
Val: 4
Ha disminuido su posición en 10 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Validar varios input text con for()

Publicado por Victor (2 intervenciones) el 05/06/2018 17:59:22
Muchas gracias, XVE. Ya ves el nivel básico que tengo que no sabía eso. Un saludo
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