Validate input data in an array of html inputs, using javascript

0

I was trying to validate 3 different arrays of inputs before processing them, where each contains 2 types of data: a note or rating and a no. of absences. But only interested to do validation in the note or qualification, entered: that is between the range of 5-10 (Leave in the condition: if ((p < 5 & p! = 0.0)), because in the table from the bd. the note or evaluation field is of a decimal type and 0.0 is displayed as default, and this value is retrieved like this in the form.)

Here is an example in php of how the fixes work, of what I was trying to do but with js. link

I tried to do the validation in this way, but I do not execute it, I think I'll be wrong from the way I call the function:

<script type="text/javascript">
function validaForm() {
   var p1 = document.getElementsByName('parcialP1[]');
   var p2 = document.getElementsByName('parcialP2[]');
   var p3 = document.getElementsByName('parcialP3[]');
     // estructura: un solo arreglo contiene dos tipo de dato: nota e inasistencias 
    // nota,           inasistencias    nota             inasistencias
   // parcialP1[0]=4,  parcialP1[2]=3,  parcialP1[3]=9,  parcialP1[4]=1
   for ( var i=1; n = p1.length; i < n; i +=2) {  //pre-incremento de 2  y bucle para primer parcial
    
         var p = p1[i-1].value; //recorre una posision en el array qe corresponda al dato: nota
         if  ( (p < 5 && p != 0.0) || (p > 10) ) {  
             alert("Nota Ingresada NO Invalida, Por Favor Ingrese Valor entre 5 y 10");
             p1.focus(); //que pusiera un focus sobre el elemnto input del array que detecta como no valido
             return false;
         }
       }

      for ( var i=1; n = p2.length; i < n; i +=2) {  //pre-incremento de 2  y bucle para primer parcial
    
         var p = p2[i-1].value; 
         if  ( (p < 5 && p != 0.0) || (p > 10) ) {  
             alert("Nota Ingresada NO Invalida, Por Favor Ingrese Valor entre 5 y 10");
             p2.focus(); //que pusiera un focus sobre el elemnto input del array que detecta como no valido
             return false;
         }
       }

       for ( var i=1; n = p3.length; i < n; i +=2) {  //pre-incremento de 2  y bucle para primer parcial
    
         var p = p3[i-1].value; 
         if  ( (p < 5 && p != 0.0) || (p > 10) ) {  
             alert("Nota Ingresada NO Invalida, Por Favor Ingrese Valor entre 5 y 10");
             p3.focus(); //que pusiera un focus sobre el elemnto input del array que detecta como no valido
             return false;
         }
       }
 }
</script>
 <form method="POST" action="Capt_Calif.php" onsubmit="return validaForm();">
          <td><input type="text" name="parcialP1[]" class="p1" size="2" value="<?php echo $alumno[2]; ?>" ></td>
          <td><input type="text" name="parcialP1[]" class="p1" size="2" value="<?php echo $alumno[3]; ?>" ></td>
          <td><input type="text" name="parcialP2[]" class="p2" size="2" value="<?php echo $alumno[4]; ?>" ></td>
          <td><input type="text" name="parcialP2[]" class="p2" size="2" value="<?php echo $alumno[5]; ?>" ></td>
          <td><input type="text" name="parcialP3[]" class="p3" size="2" value="<?php echo $alumno[8]; ?>" ></td>
          <td><input type="text" name="parcialP3[]" class="p3" size="2" value="<?php echo $alumno[9]; ?>" ></td>
        </tr>
            </tbody>
           </table>   
           <div align="center"> 
           <button type="submit" name="formAlumno" id="enviar" class="btn btn-success" >CAPTURAR EVALUACIONES</button>
    
asked by Armando Arellano 22.11.2016 в 20:44
source

2 answers

1

I think I have read this question on occasion. At first glance, the first thing I notice is that document.getElementsByName receives as a parameter a string with the name of the elements that you want to obtain while you are passing an undefined variable. Then in the var p = px[i-1] declarations you must use the value property to access the value, like this: var p = p2[i-1].value .

    
answered by 22.11.2016 / 23:52
source
0

The problem you have is syntax. The for has three parts defined and separated by a; In the case of your for, there are four ...

    
answered by 24.11.2016 в 00:14