Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/05/2011, 07:46
Avatar de Naahuel
Naahuel
 
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 1 mes
Puntos: 192
Respuesta: Validacion de formulario sin alert

Yo creo que sería mejor asignar la función para cuando el form sea enviado. Es decir para el evento "onsubmit". Mirá este ejemplo:
Código HTML:
Ver original
  1.     <head>
  2.         <title></title>
  3.        
  4.         <script>
  5.        
  6.             function valida(){
  7.                 var obj = document.getElementById('errorusuario');
  8.                 obj.innerHTML="Minimo 4 caracteres";
  9.                 //return false si hay error, return true si no lo hay
  10.                 return false;
  11.             }
  12.        
  13.         </script>
  14.                
  15.     </head>
  16.    
  17.     <body onsubmit="return valida()">
  18.    
  19.         <form name="formusu">
  20.             <div id="errorusuario"></div>
  21.             <input type="text" />
  22.             <input type="submit" value="Validar" />
  23.         </form>
  24.     </body>
  25. </html>

Nesitás tener un input de tipo submit que llamará a la función. Si la función devuelve FALSE el form no es enviado. Si no hay errores la función valida() debe devolver TRUE y entonces el form se envía normalmente.
__________________
nahueljose.com.ar