Ver Mensaje Individual
  #5 (permalink)  
Antiguo 09/03/2014, 08:00
Pantaláimon
 
Fecha de Ingreso: julio-2006
Ubicación: Barcelona
Mensajes: 244
Antigüedad: 17 años, 10 meses
Puntos: 32
Respuesta: Opinion validación javascript

Cierto, los innerHTML cambian (que por cierto, si sólo quieres añadir texto usa mejor textContent ). Pero vaya, la idea es la misma, poner la información que cambia en un objeto o array y recorrer la estructura que se repite mediante un bucle:

Código Javascript:
Ver original
  1. var info = {
  2.         name        : ['namebox'        , 'Please fill in name' ],
  3.         passport    : ['passportbox'    , 'Please fill in your identification' ],
  4.         emailtrue   : ['emailtruebox'   , 'Please fill in email' ],
  5.         repeat_email: ['repeat_emailbox', 'Please repeat email' ],
  6.         telmobile   : ['telmobilebox'   , 'Please fill in mobile to bring on holiday' ],
  7.         test        : ['resultbooking'  , 'Please answer the security question' ]
  8.     }
  9.     for( key in inputs ) {
  10.         if(document.Booking[key].value == '')
  11.         {
  12.             divresult = document.getElementById(info[key][0])
  13.             divresult.className = 'validation'
  14.             divresult.textContent = info[key][1];
  15.             document.Booking[key].focus();
  16.             return false;
  17.         }
  18.     }

preventDefault es un método de javascript. Aquí dejo información;
http://www.w3.org/TR/2003/NOTE-DOM-L...t-binding.html
http://www.lewebmonster.com/funcion-...en-javascript/
https://developer.mozilla.org/es/doc...preventDefault

La idea básica es que el handler( función llamada cuando ocurre el evento) acepta un parámetro event y este puede llamar al método preventDefault para evitar el submit:

Código Javascript:
Ver original
  1. function OnSubmitBooking( event ) {
  2.     event.preventDefault();
  3.     if( error validacion ) {
  4.         // ...
  5.     } else { // si todo correcto
  6.         // hacer lo que sea
  7.         // ...
  8.         // hacer submit al formulario
  9.         this.submit();
  10.     }
  11. }

Aunque sin saber del HTML quizá esté dando palos de ciego.

Un saludo!
__________________
github.com/xgbuils | npm/xgbuils

Última edición por Pantaláimon; 09/03/2014 a las 08:07