Ver Mensaje Individual
  #4 (permalink)  
Antiguo 24/03/2013, 15:23
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Error validación de formulario

A ver vamos a ordenarlo un poco, hay varias formas de hacer esto

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6.  
  7. <style type="text/css">
  8. /*<![CDATA[*/
  9.  
  10. label{
  11. width: 150px;
  12. display: inline-block;
  13. }
  14.  
  15. /*]]>*/
  16. <script type="text/javascript">
  17.  
  18. function validar(){
  19.     // menos el de email. limpiamos los campos con la funcion limpiar()
  20. var nombre = limpiar(document.getElementById('nombre').value);
  21. var email = document.getElementById('email').value;
  22. var asunto = limpiar(document.getElementById('asunto').value);
  23. var mensaje = limpiar(document.getElementById('mensaje').value);
  24. var formato = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; // expresion para validar email
  25.  
  26. if(nombre == ""){
  27. alert('Ingrese su nombre')
  28. return false; // devolvés false si hay error
  29. }
  30. var v_email = formato.test(email);
  31.  
  32. if((v_email != true)||(email == "")){ // test() devuelve true solo si se verifica la regExp
  33. alert('Email no válido');
  34. return false;
  35. }
  36.  
  37. if(asunto == ""){
  38. alert('Ingrese el asunto')
  39. return false;
  40. }
  41.  
  42. if(mensaje == ""){
  43. alert('Ingrese el mensaje')
  44. return false;
  45. }
  46.  
  47.  
  48. /// Si no hubo ningún error, el form se procesa y se envía aal action del form
  49.  
  50. alert('gracias ' + nombre);
  51.  
  52. }
  53.  
  54.  
  55. function limpiar(valor){
  56. var valor_campo = valor.replace(/^\s+/g,'').replace(/\s+$/g,'');// limpias espacios en blanco al inicio y final del nombre
  57. return valor_campo;
  58. }
  59.  
  60. </script>
  61. </head>
  62. <div>
  63. <form action="#" onsubmit="return validar();">
  64. <label for="nombre">Nombre: </label><input type="text" value="" id="nombre" name="nombre" /><br />
  65. <label for="email">Email: </label><input type="text" value="" id="email" name="email" /><br />
  66. <label for="asunto">Asunto: </label><input type="text" id="asunto" name="asunto" /><br />
  67. <label for="mensaje">Mensaje: </label><input type="text" id="mensaje" name="mensaje" /><br />
  68. <input type="submit" value="enviar" />
  69. </form>
  70. </div>
  71. </body>
  72. </html>

Asi te debería funcionar
Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.