Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/11/2017, 08:49
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Validar formulario sin saber los campos que hay

Yo haría algo así, no lo probé porque ando sin tiempo, lo hice muy, muy, rápido, pero espero se entienda la ídea y se acomode, ojo tomé arbitraríamente que si los valores de checkboxs, radios y combos no tienen valores asignados, igual se toma como no ingresado, puede pasar que no tengan valor pero si esten si hayan sido operados, así que ojo ahí:

Código Javascript:
Ver original
  1. function allFill(id, selectDefaultNeeded /* üse default value of selects as is not filled form */, selectFirstDefault /* in select the fist option is consider default */){
  2. var form = document.getElementById(id);
  3. var inputs = form.getElementsByTagName("input");
  4. var radios = new Array(); //neccessary for radios checks
  5.  
  6.     for(var k=0;k<inputs.length;k++){ //check for inputs
  7.     var input = inputs[k];
  8.     var type = input.getAttribute("type");
  9.  
  10.         if(input.value == ""){ return false }
  11.        
  12.         if(type == "checkbox" && !input[k].checked){ return false; }//for checkbox
  13.        
  14.         if(type == "radio"){ //for radios, this is quitely complicated
  15.         var name = input.getAttribute("name"); //getting the radio name group
  16.         var passthru = false; //aux to se if the radio groud will pass the exam
  17.             if(radios.indexOf(name) == -1){ //if the groups is not checked yet
  18.             var rds = form.getElementsByName(name); //select all elements with the radio group name
  19.                 for(var n=0;n<rds.length;n++){
  20.                 var rtype = rds[k].getAttribute("radio"); //this is just for security...
  21.                     if(rtype == "radio" && rds[k].checked){  //if checked...
  22.                     radios[radios.length] = name; //add the name to radios groups checked
  23.                     passthru = true; //the group has passed the exam
  24.                     break; //go away
  25.                     }
  26.                 }
  27.             }
  28.            
  29.             if(!passthru){ //so.. if the groups doesn't pass the exam
  30.             return false;
  31.             }
  32.         }
  33.     }
  34.  
  35. var ta = form.getElementsByTagName("textarea"); for(var k=0; k<ta.length; k++){ if(ta.value == ""){ return false; } } //for textarea
  36.    
  37. var selects = form.getElmeentsByTagName("select");
  38.  
  39.     for(var k=0;k<selects.length;k++){ //for select, again quitely complicated
  40.        
  41.         if(selects[k].value == ""){ return false; } //obiusly this first
  42.    
  43.         if(selectDefaultNeeded){ //if default is needed
  44.         var opts = selects.getElementsByTagName("option"); //get ops
  45.         var defaultValue = ""; //set default value
  46.        
  47.            
  48.             for(var n=0;n<opts.length;n++){ //getting here the default -if exists- option. attribute (selected)
  49.                 if(opts[n].hasAttribute("selected")){
  50.                 defaultValue = opts[k].value;
  51.                 break;
  52.                 }
  53.             }
  54.            
  55.             if(selectFirstDefault && defaultValue == ""){ defaultValue == opts[0].value; } //this is you use first value as default
  56.  
  57.             if(selects[k].value == defaultValue || defaultValue == ""){ return false; } //if the default is used + empty security value
  58.            
  59.         }
  60.     }
  61. return true;
  62. }

Parametros:

id: id del form
selectDefaultNeeded (true/false): Usar el valor por defecto de un combo, como si no estubiera ingresado, (atributo selected)
selectFirstDefault (true/false): Si el anterior es true, usar la primera opcion como defecto

PS: disculpa si me explique mal en todo este post repito vine de pasa
PS2: te todas formas valida en back-end <----------edite esto

Última edición por alvaro_trewhela; 13/11/2017 a las 08:57