validate data except the selector that triggers Jquery

0

I want it to validate when it leaves the field if the same data is not repeated in another input. Here my code and my progress

<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">
<input type="text" class="num_valido">

<script>
$(document).on('blur','.num_valido',function(){
  var value = $(this).val(); //SACAMOS EL VALOR
var that = this;
  if (!$.isNumeric(value)) //VALIDAMOS SI ES UN NUMERO
  {
    $(this).val($(this).data('')); //NO LO ES LO DEJAMOS EN BLANCO
  }

  else
  {
    if (value <= 0 || value > 10) //EL NUMERO DEBE ESTAR ENTRE 1 Y 10
    {
      $(this).val($(this).data('')); // SI NO LO DEJAMOS EN BLANCO
    }

    else
    {
      $('.num_valido').each(function(){
        if($(this).val() == value)
        {
          $(this).val($(this).data('')); //LO DEJAMOS EN BLANCO SI YA ESTA EN OTRO input
          return false; //CERRAMOS EL CICLO
        }
      });
    }
  }
});
</script>

The problem is that it is also validating the same field where it is detecting the blur, and obviously it is cleaning the field because if it exists in the path of the "num_valido". How do I do to validate all of them except the one I am triggering the blur?

EDIT ANSWER

  $('.num_valido').each(function(){
    if (that != this)
    {
      if ($(this).val() == value)
      {
          $(this).val($(this).data(''));
          return false;
      }
    }
  });

I differentiated that from this above I declared it. May it serve you. Thanks to yorodm who gave me the idea.

    
asked by Alberto Siurob 05.06.2017 в 21:52
source

1 answer

1

The problem is that when you do this:

$('.num_valido').each(function(){
        if($(this).val() == value)
        {
          $(this).val($(this).data('')); //LO DEJAMOS EN BLANCO SI YA ESTA EN OTRO input
          return false; //CERRAMOS EL CICLO
        }
      });

You also go through the element that launched the blur . Since before running the iteration you have to have this as the reference to the element of blur you could take advantage of and do something like:

   that = this; // para guardar el actual
   elementos_a_verificar = $.grep($('.num_valido'),function(n,i){
     // filtrar aqui utilizando that
     return ! $(that).is(n);
   });

and then iterate over elementos_a_verificar . I have my doubts about the use of is in this context but that is the closest thing to the equality operator that serves as selectors.

    
answered by 05.06.2017 / 22:13
source