Validate an array of inputs that does not allow the form to be processed, with values entered between (1-4) or greater than 10

0

   <form method="POST" action="Capt_Calif.php" onsubmit="return valida(parcial)">
         <!-- arreglos usando class para ser llamados por js, p1=calif de parcial1  -->
          <td><input type="text" name="parcialP1[]" class="p1" size="3" value="<?php echo $alumno[2]; ?>" ></td>
          <td><input type="text" name="parcialP1[]" class="ip1" size="3" value="<?php echo $alumno[3]; ?>" ></td>
          <td><input type="text" name="parcialP2[]" class="p2" size="3" value="<?php echo $alumno[4]; ?>" ></td>
          <td><input type="text" name="parcialP2[]" class="ip2" size="3" value="<?php echo $alumno[5]; ?>" ></td>
          <td><input type="text" name="parcialP3[]" class="p3" size="3" value="<?php echo $alumno[8]; ?>" ></td>
          <td><input type="text" name="parcialP3[]" class="ip3" size="3" value="<?php echo $alumno[9]; ?>" ></td>

          <td><input type="hidden" name="idgpo[]" value="<?php echo $alumno[10]; ?>" readonly></td>
          <td><input type="hidden" name="idalumno[]" value="<?php echo $alumno[7]; ?>" readonly></td>
     
          <td><input type="text" name="final[]" class="final" size="3" value="<?php echo $alumno[11]; ?>" readonly ></td>
          <td><input type="text" name="porcent[]" class="final" size="3" value="<?php echo $alumno[12]; ?>" readonly ></td>
          <td><input type="text" name="asistotales[]" class="inatot" size="3" value="" ></td>
</form>
 <script type="text/javascript">
  function valida(parcial) {
  var i,
  inputs,
  inputsTotal,
  inputsIndex;

  for (i=0; i < parcial; i++) {
  inputs = document.getElementsByClassName(parcial[i]);
  var n = inputs.length;
  inputsIndex = 0;
  inputsTotal = inputs.length;

  for(inputsIndex; inputsIndex < inputsTotal; inputsIndex++) {
  p1 = inputs[i];
  p2 = inputs[i];
  p3 = inputs[i];
     //////////////////////////////////////// PARA ARREGLO PARCIAL2
     if (p1 = 4 || p1 = 3)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p1 = 2 || p1 = 1)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p1 > 10)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
    //////////////////////////////////////// PARA ARREGLO PARCIAL1
     if (p2 = 4 || p2 = 3)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p2 = 2 || p2 = 1)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p2 > 10)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     //////////////////////////////////////// PARA ARREGLO PARCIAL3
     if (p3 = 4 || p3 = 3)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p3 = 2 || p3 = 1)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }
     if (p3 > 10)  {
        alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        return false;
     }

    }//segundo for
   }//primer for
 }
</script>

<script type = "text/javascript" >
window.onload = function() { //este metodo esta, mal, algo falta
parcial(['p1', 'p2', 'p3']);
}
</script>
    
asked by Armando Arellano 08.11.2016 в 17:24
source

2 answers

0

I saw that besides misusing the signs = that is assignment when it should be == to compare the values. You do many if for the same error, since you in the comments said number between (1-4) or greater than 10 you already know the proper condition. Second, it is easier to go through an arrangement than to do so much code for something that can be solved with a variable flag

I leave an ordinary table for this example:

<table id="example">
    <thead>
        <tr>
            <th>Rendering engine</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input name="parcialP1[]" size="3" type="text" class="evaluacion">
            </td>
        </tr>
        <tr>
            <td>
                <input name="parcialP1[]" size="3" type="text" class="inasistencia">
            </td>
        </tr>
        <tr>
            <td>
                <input name="parcialP2[]" size="3" type="text" class="evaluacion">
            </td>
        </tr>
        <tr>
            <td>
                <input name="parcialP2[]" size="3" type="text" class="inasistencia">
            </td>
        </tr>
    </tbody>
</table>
<input type="button" id="enviar" value="Enviar">

In the code JS I assume that you click to make the post of the form, I create a variable flag called error and initialized in false . I do a each fix per array and compare its value

EDIT

As you now explained that the inputs of a array are different and you only want to evaluate 1 in particular, I will add to the inputs a class, class evaluacion and inasistencia .

$(document).ready(function(e) {
    $("#enviar").click(function() {
        var error = false;
        $('input[name^="parcialP1"]').each(function() {
            if($(this).attr('class') == "evaluacion"){
                var valor = $(this).val();
                if (valor < 5 || valor > 10 ) {
                    error = true;
                    return;
                }
            }
        });
        $('input[name^="parcialP2"]').each(function() {
            if($(this).attr('class') == "evaluacion"){
                var valor = $(this).val();
                if (valor < 5 || valor > 10) {
                    error = true;
                    return;
                }
            }
        });
        if(error){
           e.preventDefault();
           alert('Escala Incorrecta: Ingrese Evaluación entre: 5 - 10');
        }
    });
});
    
answered by 08.11.2016 / 17:59
source
0

To start, you must change parcial by parcial.length in the first for . Why do you use n if you already have inputsTotal ? You have to access the values of each input using the property value , you can not access them directly: pn = inputs[i].value . Lastly, you are making assignments where you must make comparisons, you must change pn = x , by pn == x in all if .

    
answered by 08.11.2016 в 17:45