Validate form if variable1 is greater than variable2

Asked

Viewed 73 times

0

Hello, I am with creating a form where the person should provide two values, a lower limit and an upper one, if the lower limit is higher than the upper one I would like an error message to be generated! before the form is sent. I managed to make it "work", but when I enter even numbers in the first input, the validation is lost and returns as if nothing was typed (last).

var input2 = $('#limiteinf');
var input3 = $('#limitesup');
var saida2 = $('.help-block2');

input2.on('input', function() {
  atualiza2();
});
input3.on('input', function() {
  atualiza2();
});

function atualiza2() {
  var inferior2 = $("#limiteinf").val();
  var superior2 = $("#limitesup").val();
  var inferior = parseFloat(inferior2);
  var superior = parseFloat(superior2);
  if (inferior & superior != '') {

    if (inferior > superior) {
      $(".help-block2").html("O limite inferior deve ser menor que o Superior!");
      $('#validalimite').removeClass("has-success");
      $('#validalimite').addClass('has-error');
    }
    // Se resposta for false, ou seja, não ocorreu nenhum erro
    else {
      // Exibe mensagem de sucesso
      $(".help-block2").html("");
      $('#validalimite').removeClass("has-error");
      $('#validalimite').addClass('has-success');
      // Coloca a mensagem no div de mensagens
    }
  } else {
    $('#validalimite').removeClass("has-success");
    $('#validalimite').removeClass("has-error");
    $('#validalimite').addClass('has-error');
    $(".help-block2").html("Informe os LIMITES!");
  }

  // saida.html('exibe resultado...');
  //aqui você pode chamar a função ajax
}
}
<input id="limiteinf" placeholder="Limite inferior" pattern="[0-9]+$"></input>
<span>e</span>
<input id="limitesup" placeholder="Limite superior" pattern="[0-9]+$"></input>
<p class="help-block2"></p>

  • But the event is working right?

  • Your code has syntax errors. You can fix?

  • the worst that with odd number was working, I didn’t even call to see &&, but as Abriel reported, it was missing another &. Anyway, thanks for the help.

1 answer

1


The logical operator and if the first one is not correct, so it is always falling in Else.

The right thing would be && instead of &.

It also seems that there is a } unnecessary at the end.

  • Wow, to think I stayed yesterday looking for what I had done wrong -.- Can’t do things with sleep anyway, thanks @Gabriel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.