Formularios HTML dividirlo por pasos, jQuery

Revisando un poco la red, buscaba algo que me dividiera un formulario HTML en etapas o paso con el menor esfuerzo posible.
En la web de anieto2k he encontrado la soluci贸n a las cual le aplique un par de cambios para hacer validaciones y ha quedado muy buena. Est谩 se utiliza con jQuery.
Simplemente es una funci贸n que se encarga de convertir todos los formularios cuyo atributo class sea “dividirPasos”. 脷nicamente se encarga de cortar el formulario por elementos <fieldset /> e implementar validaciones para cada uno de los pasos.

La funci贸n "stepForm" agrega los botones “Volver” y “Seguir“, asociando al evento Onclick un funci贸n pasando como par谩metro el paso actual.

Funci贸n que aplica los pasos y las validaciones:


<script type="text/javascript">
  jQuery.extend({
      stepForm: function(txtBack, txtNext, token){
      var fieldsets = $((token || 'fieldset'), $("form.dividirPasos"));
      var total = $(fieldsets).length;
       $(fieldsets).each(function(x,el){
       if (x > 0) {
          $(el).hide();
          $(el).append('<a onClick="validarEtapaVolver('+(x)+')" href="#">'+ (txtBack || 'Volver') +'</a>');
       }
       if ((x+1)< total) {
          $(el).append('<a onClick="validarEtapaSiguiente('+(x)+')" href="#">'+ (txtNext || 'Seguir')+'</a>');
       }
        $(el).attr("id", "x_" + x);
    });
  }
});

function validarEtapaVolver(x){
    var volver = false;
    if(x == 0){
      alert("VOLVER: Validar la etapa 0");
      volver = true;
    }
    if(x == 1){
      alert("VOLVER: Validar la etapa 1");
      volver = true;
    }
    if(x == 2){
      alert("VOLVER: Validar la etapa 2");
      volver = true;
    }

   //SI TODO ES CORRECTO vuelvo una etapa
    if(volver){
       $("#x_" + (x-1)).show();
       $("#x_" + (x)).hide();
    }
}

function validarEtapaSiguiente(x){
    var sigue = false;
    if(x == 0){
      alert("SIGUIENTE: Validar la etapa 0");
      sigue = true;
    }
    if(x == 1){
      alert("SIGUIENTE: Validar la etapa 1");
      sigue= true;
    }
    if(x == 2){
       alert("SIGUIENTE: Validar la etapa 2");
       sigue = true;
    }

   //SI TODO ES CORRECTO voy a la siguiente etapa
    if(sigue){
       $("#x_" + (x+1)).show();
       $("#x_" + (x)).hide();
    }
}

//inicializador
   $(document).ready(function() {
      $.stepForm();
   });
</script>

Formulario HTML con los <fieldset /> :


<form name="form" action="" method="post" class="dividirPasos">
 <fieldset><!--paso 1-->
     <legend>Datos de Usuario</legend>
       <table>
         <tr>
            <td><label for="name">Nombre:</label></td>
            <td><input name="nombre" id="name" value="" /></td>
         </tr>
         <tr>
             <td><label for="surname">Apellidos:</label></td>
             <td><input name="apellidos" id="surname" value="" /></td>
         </tr>
         <tr>
             <td><label for="birthday">Fecha de Nacimiento:</label></td>
             <td><input name="apellidos" id="surname" value="" /></td>
         </tr>
         <tr>
             <td><label for="email">E-mail:</label></td>
             <td><input name="email" id="email" value="" /></td>
         </tr>
      </table>
 </fieldset>

 <fieldset><!--paso 2-->
    <legend>Otros Datos de Usuario</legend>
      <table>
        <tr>
           <td><label for="text1">Que te gusta?:</label> </td>
           <td>
                 <select聽聽 聽id="text1">
                      <option>gato</option>
                      <option>perro</option>
                      <option>chocolate</option>
                      <option>frutilla</option>
                 </select>
           </td>
        </tr>
        <tr>
           <td> <label for="text2">Tenes Hermanos: </label> </td>
           <td> Si?<input type="checkbox" id="text2" /> </td>
        </tr>
      </table>
 </fieldset>

 <fieldset><!--paso 3-->
    <legend>脷ltimo Paso聽 </legend>
       <label for="submit">Si est谩s seguro que completaste los datos de forma correcta, presiona el bot贸n  Enviar<br /></label>
       <input聽聽 聽type="submit" id="submit" value="Enviar" /><br />
 </fieldset>

</form>

Enlaces de inter茅s:

Saludos

promero



驴Disfrutaste esta entrada? Por qu茅 no dejas un comentario abajo y contin煤as la conversaci贸n, o te suscr铆bes a mi feed y obtienes los art铆culos como este enviados autom谩ticamente cada d铆a hacia tu lector de feeds.

Trackbacks & Pingbacks

No hay trackbacks/pingbacks todav铆a.

Comentarios

muy bueno Pablito, lo que andaba buscando.. ;)

Deja un comentario

Saltos de l铆nea autom谩ticos, la direcci贸n de email nunca ser谩 publicada, HTML permitido: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(requerido)

(requerido)