Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: janisorna en 30 Diciembre 2011, 17:36 pm



Título: Problema con la validación de un formulario con javascript
Publicado por: janisorna en 30 Diciembre 2011, 17:36 pm
Hola a todos, he echo un formulario i ahora quiero validar sus campos pero quando le doy a enviar no me sale la alerta de que te dejas un campo por rellenar.
Aquí dejo el codigo:
Porfavor ayudadme!!!


Código
  1. <html>
  2. <head>
  3. <title>Formulario para el curso</title>
  4. <script>
  5. function valida_envia(){
  6. //valido el nombre
  7. if (document.fvalida.nombre.value.length==0){
  8. alert("Tiene que escribir su nombre")
  9. document.fvalida.nombre.focus()
  10. return0;
  11. }
  12. //valido la edad. Tiene que ser entero mayor que 18
  13. edad = document.fvalida.edad.value
  14. edad = ValidarEntero(edat)
  15. document.fvalida.edad.value=edat
  16. if(edat==""){
  17. alert("Tiene que indicar su edad")
  18. document.fvalida.edad.focus()
  19. return 0;
  20. }else{
  21. if (edad<18){
  22. alert("Debe ser mayor de 18 años.")
  23. document.fvalida.edad.focus()
  24. return 0;
  25. }}
  26. //valido el correo electrónico
  27. if (document.fvalida.email.value.length==o){
  28. alert("Tiene que poner su correo electrónico")
  29. document.fvalida.email.focus()
  30. return 0;
  31. }
  32. //valido el teléfono móvil.
  33. telm = document.fvalida.telm.value
  34. telm = ValidarEntero(telm)
  35. document.fvalida.telm.value=telm
  36. if(telm==""){
  37. alert("Tiene que poner su teléfono móvil")
  38. document.fvalida.telm.focus()
  39. return 0;
  40. }
  41. //valido el telefono fijo.
  42. telf = document.fvalida.telf.value
  43. telf = ValidarEntero(telf)
  44. document.fvalida.telf.value=telf
  45. if(telf==""){
  46. alert("Tiene que poner su teléfono fijo")
  47. document.fvalida.telf.focus()
  48. return 0;
  49. //valio si tienes experiencia.
  50. if(document.fvalida.experiencia.value.length==0){
  51. alert("Tienes que rellenar el campo de si tienes experiencia")
  52. document.fvalida.experiencia.focus()
  53. return 0;
  54. }
  55. //valio como te has enterado.
  56. if(document.fvalida.curso.value.length==0){
  57. alert("Tienes que rellenar el campo de com te has enterado del curso")
  58. document.fvalida.curso.focus()
  59. return 0;
  60. }
  61. //valido si tomas algún medicamento.
  62. if(document.fvalida.medicamentos.value.length==0){
  63. alert("Tienes que rellenar el campo de si tomas algun medicamento.")
  64. document.fvalida.medicamentos.focus()
  65. return 0;
  66. }
  67. </script>
  68. </head>
  69. <body>
  70. <form name="fvalida" method="post" action="enviar2.php">
  71. Nombre:
  72. <input type="text" name="nombre" size="12">
  73. <br>
  74. <br>
  75. Edad:
  76. <input type="text" name="edad" size="12" maxlength="2">
  77. <br>
  78. <br>
  79. Correo electrónico:
  80. <input type="text" name="email" size="12">
  81. <br>
  82. <br>
  83. Teléfono Móvil:
  84. <input type="text" name="telm" size="12" maxlength="9">
  85. <br>
  86. <br>
  87. Teléfon Fijo:
  88. <input type="text" name="telf" size="12" maxlength="9">
  89. <br>
  90. <br>
  91. <table>
  92. <tr>
  93. <td>
  94. ¿Tienes coche?
  95. </td>
  96. <td>
  97. <input type="radio" name="coche"
  98. value="sí">
  99. Si.<br>
  100. <input type="radio" name="coche"
  101. value="no">
  102. No.<br>
  103. </td>
  104. </tr>
  105. </table>
  106. <br>
  107. <br>
  108. <table>
  109. <tr>
  110. <td colspan="2" valign="top">
  111. ¿Tienes alguna experiencia?</td>
  112. <td><textarea name="experiencia" cols="30 rows="5">
  113. </textarea>
  114. </td>
  115. </tr>
  116. </table>
  117. <br>
  118. <br>
  119. <table>
  120. <tr>
  121. <td colspan="2" valign="top">
  122. ¿Como te has enterado del curso?</td>
  123. <td><textarea name="curso" cols="30" rows="5">
  124. </textarea>
  125. </td>
  126. </tr>
  127. </table>
  128. <br>
  129. <br>
  130. <table>
  131. <tr>
  132. <td>
  133. ¿Eres Vegetariano/a?
  134. </td>
  135. <td>
  136. <input type="radio" name="vegeteriano"
  137. value="">
  138. Si.<br>
  139. <input type="radio" name="vegetariano"
  140. value="no">
  141. No.<br>
  142. </td>
  143. </tr>
  144. </table>
  145. <br>
  146. <br>
  147. <table>
  148. <tr>
  149. <td colspan="2" valign="top">
  150. ¿Tomas algún medicamento? ¿Qual?</td>
  151. <td><textarea name="medicamentos" cols="30" rows="5">
  152. </textarea>
  153. </td>
  154. </tr>
  155. </table>
  156. <br>
  157. <br>
  158. <br>
  159. <input type="button" value="Enviar" onclick="valida_envia()">
  160. <input type="reset" value="Borrar" name="borrar">
  161. </table>
  162. </form>
  163. </body>
  164. </html>


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: Leyer en 30 Diciembre 2011, 19:16 pm
Donde esta la funcion ValidarEntero? ademas de que tienes errores en los nombre de las variables colocaste edat y es edad. y cosas  como return0;  :xD


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: janisorna en 30 Diciembre 2011, 21:28 pm
Donde esta la funcion ValidarEntero? ademas de que tienes errores en los nombre de las variables colocaste edat y es edad. y cosas  como return0;  :xD

He arreglado lo que dices pero continua sin funcionarme.

Si no lo conseguimos arreglar si alguien sabe otra forma de validarlo que lo diga porfavor :) :)


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: Leyer en 30 Diciembre 2011, 21:44 pm
Falta colocar la funcion validarnumero

Código
  1. function valida_envia(){
  2. //valido el nombre
  3. if (document.fvalida.nombre.value.length==0){
  4. alert("Tiene que escribir su nombre")
  5. document.fvalida.nombre.focus()
  6. return 0;
  7. }
  8. //valido la edad. Tiene que ser entero mayor que 18
  9. edad = document.fvalida.edad.value
  10. if(edad==""){
  11. alert("Tiene que indicar su edad")
  12. document.fvalida.edad.focus()
  13. return 0;
  14. }else{
  15. if (edad<18){
  16. alert("Debe ser mayor de 18 años.")
  17. document.fvalida.edad.focus()
  18. return 0;
  19.  }
  20. }
  21.  
  22. //valido el correo electrónico
  23. if (document.fvalida.email.value.length==0){
  24. alert("Tiene que poner su correo electrónico")
  25. document.fvalida.email.focus()
  26. return 0;
  27. }
  28. //valido el teléfono móvil.
  29. telm = document.fvalida.telm.value
  30. document.fvalida.telm.value=telm
  31. if(telm==""){
  32. alert("Tiene que poner su teléfono móvil")
  33. document.fvalida.telm.focus()
  34. return 0;
  35. }
  36. //valido el telefono fijo.
  37. telf = document.fvalida.telf.value
  38. document.fvalida.telf.value=telf
  39. if(telf==""){
  40. alert("Tiene que poner su teléfono fijo")
  41. document.fvalida.telf.focus()
  42. return 0;
  43. }
  44. //valio si tienes experiencia.
  45. if(document.fvalida.experiencia.value.length==0){
  46. alert("Tienes que rellenar el campo de si tienes experiencia")
  47. document.fvalida.experiencia.focus()
  48. return 0;
  49. }
  50. //valio como te has enterado.
  51. if(document.fvalida.curso.value.length==0){
  52. alert("Tienes que rellenar el campo de com te has enterado del curso")
  53. document.fvalida.curso.focus()
  54. return 0;
  55. }
  56. //valido si tomas algún medicamento.
  57. if(document.fvalida.medicamentos.value.length==0){
  58. alert("Tienes que rellenar el campo de si tomas algun medicamento.")
  59. document.fvalida.medicamentos.focus()
  60. return 0;
  61. }
  62.  
  63. }


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: KuraraGNU en 30 Diciembre 2011, 21:45 pm
Ahí tienes fallos de comillas abiertas pero no cerradas, fíjate que tu código se vuelve azul cuando lo posteas:

Código:
<td><textarea name="experiencia" cols="30 rows="5">

A veces son tonterías por lo que no nos salen las cosas, mira a ver si es una chorrada como esa.
<td><textarea name="experiencia" cols="30 rows='5'">


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: janisorna en 31 Diciembre 2011, 11:42 am
Mirad ahora lo tengo de esta manera pero sigue sin funcionarme:

Citar
<html>
<head>
<title>Formulario para el curso</title>
<script language="javascript">
function valida_envia(form){
//valido el nombre
if (fvalida.nombre.value.length==0){
alert('Tiene que escribir su nombre');
fvalida.nombre.focus()
return 0;
}
//valido laedad
if (fvalida.edad.value.length==0){
alert('Tiene que escribir su edad');
fvalida.edad.focus()
return 0;
}
//valido el correo electrónico
if (fvalida.email.value.length==o){
alert('Tiene que poner su correo electrónico');
fvalida.email.focus()
return 0;
}
//valido el teléfono móvil.
if (fvalida.telm.value.length==0){
alert('Tiene que escribir su Teléfono Móvil');
fvalida.telm.focus()
return 0;
}
//valido el telefono fijo.
if (fvalida.telf.value.length==0){
alert('Tiene que escribir su Teléfono Fijo');
fvalida.telf.focus()
return 0;
}
//valio si tienes experiencia.
if(fvalida.experiencia.value.length==0){
alert('Tienes que rellenar el campo de si tienes experiencia');
fvalida.experiencia.focus()
return 0;
}
//valido como te has enterado.
if(fvalida.curso.value.length==0){
alert('Tienes que rellenar el campo de com te has enterado del curso');
fvalida.curso.focus()
return 0;
}
//valido si tomas algún medicamento.
if(fvalida.medicamentos.value.length==0){
alert('Tienes que rellenar el campo de si tomas algun medicamento.');
fvalida.medicamentos.focus()
return 0;
}
</script>
</head>
<body>
<form name="fvalida" method="post" action="enviar2.php" onSumbit="valida_envia(this);">
Nombre:
<input type="text" name="nombre" size="12">
<br>
<br>
Edad:
<input type="text" name="edad" size="12" maxlength="2">
<br>
<br>
Correo electrónico:
<input type="text" name="email" size="12">
<br>
<br>
Teléfono Móvil:
<input type="text" name="telm" size="12" maxlength="9">
<br>
<br>
Teléfon Fijo:
<input type="text" name="telf" size="12" maxlength="9">
<br>
<br>
<table>
<tr>
<td>
¿Tienes pareja?
</td>
<td>
<input type="radio" name="pareja"
value="sí">
Si.<br>
<input type="radio" name="pareja"
value="no">
No.<br>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Tienes alguna experiencia?</td>
<td><textarea name="experiencia" cols="30 rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Como te has enterado?</td>
<td><textarea name="curso" cols="30" rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td>
¿Eres Vegetariano/a?
</td>
<td>
<input type="radio" name="vegeteriano"
value="sí">
Si.<br>
<input type="radio" name="vegetariano"
value="no">
No.<br>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Tomas algún medicamento? ¿Qual?</td>
<td><textarea name="medicamentos" cols="30" rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<br>
<br>
<input type="submit" name="enviar" value="Enviar">
<input type="reset" value="Borrar" name="borrar">
</table>
</form>
</body>
</html>


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: KuraraGNU en 31 Diciembre 2011, 22:27 pm
No se si lo has conseguido arreglar, yo ya lo he arreglado, prueba esto en este orden, quizás yo hice demasiado:

1- No has cerrado corchetes de la función, abajo del todo antes de </script> te falta uno

2- No has escrito bien onSubmit, has puesto onSumbit

3- No has cerrado los "" que te dije en mi anterior mensaje.

4- Como le entras a la función valida_envia(form) En lugar de poner
(fvalida.nombre.value.length==0)
Se debería de poner
(form.nombre.value.length==0)

Pero de todas formas, aunque salga el mensaje este, se te va a la página "enviar2.php" y no hace lo del focus() (o si lo hace lo hace tan rápido que no se ve) Tienes que poner una condición para que cuando te salte la función no se te vaya a esa página.

Ten cuidado de estas pequeñas tonterías que se evitarían si mientras lo escribes tienes cuidado de lo que escribes y de que cierras todos los corchetes que abres.


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: janisorna en 3 Enero 2012, 19:21 pm
Mira ahora lo he camviado un poco pero en teoria tiene que ir bien pero no me funciona:

Código:
<html>
<head>
<title>Formulario para el curso</title>
<script language="javascript">
function Valida(){
if(fvalida.nombre==""){alert('campo sin rellenar');return false;}
if(fvalida.edad==""){alert('campo sin rellenar');return false;}
if(fvalida.email==""){alert('campo sin rellenar');return false;}
if(fvalida.telm==""){alert('campo sin rellenar');return false;}
if(fvalida.telf==""){alert('campo sin rellenar');return false;}
if(fvalida.experiencia==""){alert('campo sin rellenar');return false;}
if(fvalida.curso==""){alert('campo sin rellenar');return false;}
if(fvalida.medicamentos==""){alert('campo sin rellenar');return false;}
}
</script>
</head>
<body>
<font face="arial">
<center><b><font size="5">Formulario</font></b></center>
<form name="fvalida" method="post" action="enviar2.php" onsubmit="return Valida()">
Nombre:
<input type="text" name="nombre" size="12">
<br>
<br>
Edad:
<input type="text" name="edad" size="12" maxlength="2">
<br>
<br>
Correo electrónico:
<input type="text" name="email" size="12">
<br>
<br>
Teléfono Móvil:
<input type="text" name="telm" size="12" maxlength="9">
<br>
<br>
Teléfon Fijo:
<input type="text" name="telf" size="12" maxlength="9">
<br>
<br>
<table>
<tr>
<td>
¿Tienes pareja?
</td>
<td>
<input type="radio" name="pareja"
value="sí">
Si.<br>
<input type="radio" name="pareja"
value="no">
No.<br>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Tienes alguna experiencia?</td>
<td><textarea name="experiencia" cols="30" rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Como te has enterado?</td>
<td><textarea name="curso" cols="30" rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<table>
<tr>
<td>
¿Eres Vegetariano/a?
</td>
<td>
<input type="radio" name="vegeteriano"
value="sí">
Si.<br>
<input type="radio" name="vegetariano"
value="no">
No.<br>
</td>
</tr>
</table>
<br>
<br>
<table>
<tr>
<td colspan="2" valign="top">
¿Tomas algún medicamento? ¿Qual?</td>
<td><textarea name="medicamentos" cols="30" rows="5">
</textarea>
</td>
</tr>
</table>
<br>
<br>
<br>
<input type="submit" name="enviar" value="Enviar">
<input type="reset" value="Borrar" name="borrar">
</table>
</form>
</font>
</body>
</html>


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: Leyer en 3 Enero 2012, 19:46 pm
:¬¬
valida.nombre.value


Título: Re: Problema con la validación de un formulario con javascript
Publicado por: janisorna en 3 Enero 2012, 20:22 pm
:¬¬
valida.nombre.value

Muchisimas gracias porfin me funciona correctamente!!!!!!!!!!  :laugh: ;D :laugh: