Varias parejas campoCantidad y botonEnviar en un solo formulario en paralelo
Publicado por SSR (2 intervenciones) el 21/06/2014 00:14:48
Hola amigos, intento crear un único formulario pero que tenga varios campos para recibir cantidad y varios botones de envio, la idea es que cada campoenvio funcione con su correspondiente botón y se realice el envio al código que procesa si es un dígito para que no se pueda introducir ni letras ni decimales.
El problema es que el código que he echo funciona correctamente con un solo campovalidar y un solo boton enviar, cuando añado más parecas de campo y botón, solo funciona la primera pareja.
¿Alguna idéa de como solucionar esto?, gracias...
El problema es que el código que he echo funciona correctamente con un solo campovalidar y un solo boton enviar, cuando añado más parecas de campo y botón, solo funciona la primera pareja.
¿Alguna idéa de como solucionar esto?, gracias...
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
<!DOCTYPE html>
<HTML lang="es">
<head>
<script type="text/javascript">
window.onload = iniciar;
function iniciar()
{
document.getElementById("enviar").addEventListener('click',validar,false);
}
function validar(eventopordefecto)
{
if (validarcampostexto && validarCantidad() && confirm("¿Deseas enviar el formulario?"))
return true;
else
{
eventopordefecto.preventDefault();
return false;
}
}
function validarcampostexto(objeto)
{
var formulario = objeto.form;
for (var i=0; i<formulario.elements.length; i++)
{
formulario.elements[i].className="";
}
for (var i=0; i<formulario.elements.length; i++)
{
if (formulario.elements[i].type == "text" && formulario.elements[i].value=="")
{
document.getElementById("errores").innerHTML="No puede estar en blanco el campo: "+formulario.elements[i].name;
formulario.elements[i].className="error";
formulario.elements[i].focus();
return false;
}
}
return true;
}
function validarCantidad()
{
var patron = /^\d*$/;
if (patron.test(document.getElementById("cantidad").value))
{
document.getElementById("cantidad").className="";
return true;
}
else
{
document.getElementById("errores").innerHTML="El formato correcto es un número";
document.getElementById("errores").focus();
document.getElementById("errores").className="error";
return false;
}
}
</script>
</head>
<body>
<form id="formulario" action="" method="post">
<p>
Cantidad: <input type="text" name="cantidad" id="cantidad" size="1" value="" /><br>
</p>
<div id="boton">
<input type="submit" id="enviar" name="enviar" style="width:125px; height:40px; font-size:22px;" value="Añadir" />
</div>
<div id="errores"></div>
<p>
Cantidad: <input type="text" name="cantidad" id="cantidad" size="1" value="" /><br>
</p>
<div id="boton">
<input type="submit" id="enviar" name="enviar" style="width:125px; height:40px; font-size:22px;" value="Añadir" />
</div>
<div id="errores"></div>
<p>
Cantidad: <input type="text" name="cantidad" id="cantidad" size="1" value="" /><br>
</p>
<div id="boton">
<input type="submit" id="enviar" name="enviar" style="width:125px; height:40px; font-size:22px;" value="Añadir" />
</div>
<div id="errores"></div>
<p>
Cantidad: <input type="text" name="cantidad" id="cantidad" size="1" value="" /><br>
</p>
<div id="boton">
<input type="submit" id="enviar" name="enviar" style="width:125px; height:40px; font-size:22px;" value="Añadir" />
</div>
<div id="errores"></div>
</form>
</body>
</html>
Valora esta pregunta


0