Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/03/2014, 06:59
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

Todo este código sigue la misma estructura:
Código Javascript:
Ver original
  1. // name - namebox
  2.     if(document.Booking.name.value == '')
  3.     {
  4.         divresult = document.getElementById('namebox')
  5.         divresult.className = 'validation'
  6.         divresult.innerHTML = 'Please fill in name';
  7.         document.Booking.name.focus();
  8.         return false;
  9.     }
  10.     // passport - passportbox
  11.     if(document.Booking.passport.value == '')
  12.     {
  13.         divresult = document.getElementById('passportbox')
  14.         divresult.className = 'validation'
  15.         divresult.innerHTML = 'Please fill in your identification';
  16.         document.Booking.passport.focus();
  17.         return false;
  18.     }
  19.     // emailtrue - emailtruebox
  20.     if(document.Booking.emailtrue.value == '')
  21.     {
  22.         divresult = document.getElementById('emailtruebox');
  23.         divresult.className = 'validation'
  24.         divresult.innerHTML = 'Please fill in email';
  25.         document.Booking.emailtrue.focus();
  26.         return false;
  27.     }
  28.     // repeat_email - repeat_emailbox
  29.     if(document.Booking.repeat_email.value == '')
  30.     {
  31.        divresult = document.getElementById('repeat_emailbox');
  32.        divresult.className = 'validation'
  33.        divresult.innerHTML = 'Please repeat email';
  34.        document.Booking.repeat_email.focus();
  35.        return false;
  36.     }
  37.     // telmobile - telmobilebox
  38.     if(document.Booking.telmobile.value == '')
  39.     {
  40.        divresult = document.getElementById('telmobilebox');
  41.        divresult.className = 'validation'
  42.        divresult.innerHTML = 'Please fill in mobile to bring on holiday';
  43.        document.Booking.telmobile.focus();
  44.        return false;
  45.     }
  46.     // test - resultbooking
  47.     if(document.Booking.test.value =='')
  48.     {
  49.         divresult = document.getElementById('resultbooking');
  50.         divresult.className = 'validation'
  51.         divresult.innerHTML = 'Please answer the security question';
  52.         document.Booking.test.focus();
  53.         return false;
  54.     }
Intenta usar bucles cuando te ocurra esto para no escribir tanto. A la vez que luego al cambiar el código HTML te será más rápido cambiar el código javascript:
Código Javascript:
Ver original
  1. var inputs = {
  2.         name        : "namebox",
  3.         passport    : "passportbox",
  4.         emailtrue   : "emailtruebox",
  5.         repeat_email: "repeat_emailbox",
  6.         telmobile   : "telmobilebox",
  7.         test        : "resultbooking"
  8.     }
  9.     for( key in inputs ) {
  10.         if(document.Booking[key].value == '')
  11.         {
  12.             divresult = document.getElementById(inputs[key])
  13.             divresult.className = 'validation'
  14.             divresult.innerHTML = 'Please fill in name';
  15.             document.Booking[key].focus();
  16.             return false;
  17.         }
  18.     }

Por otro lado, ¿return false es para evitar el submit pot defecto? Lo digo porque "return false" está en desuso. Deberías usar el método "preventDefault". Encontrarás información sobre este en tu buscador favorito.

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