Difficulties in logical OR operator

Asked

Viewed 43 times

-2

I’m having trouble assembling the logic operator OR || using javascript.

Before sending you should check whether the input file was selected or if input url was typed.

$("#form").submit(function() {
  if (($('input[type=file]').val() == "") || ($('#url').val() == "")) {
    alert('O campo Arquivo e ou URL deve ser prenchidos.');
    return false;
  }
});

ERROR: Ta requiring both fields.

Wish: That one of the two fields be of value.

  • What’s the problem you’re having? I ran the tests here with your code and it’s working normal.

  • @Victorcarnaval Ta demanding both camps.

  • 1

    Yes, you are using the logical operator || (OR / OR ), that is, when one of the cases is equal to true the if will be executed. If you want to execute if only when the two fields are empty, change the operator || for && (And / AND ).

  • @James Is demanding the two fields, as expected by if that you posted. The || implies that if one of the conditions is true, the if.

  • @Victorcarnaval This is my intention. Let it be filled either the input file or url or both.

1 answer

0

TL;DR: You’re using the wrong operator.


Your condition provides that if the URL field is empty or if the URL field is empty the code will be executed. This means that:

  • If the file field is empty, the alert will be displayed (even if the URL is valid);
  • If the URL field is empty, the alert will be displayed (even if the file is valid);
  • If both fields are empty.

What you want is to check if the two fields are empty. In this case, what you need is the logical operator and (&&):

$('form').on('submit', function(event) {
  event.preventDefault();
  
  const $nameField = $('[name="name"]');
  const $ageField = $('[name="age"]');
  
  if ($nameField.val() === '' && $nameField.val() === '') {
    alert('Inválido!');
    return;
  }
  
  alert('Válido!');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input type="text" name="name" placeholder="Nome" />
  <input type="text" name="age" placeholder="Idade" />
  <br />
  <button type="submit">Enviar</button>
</form>

PS: In relation to if, it is not necessary to compare empty in an empty string. So you can swap:

$nameVal.val() === ''

For:

!$nameVal.val()

Staying:

$('form').on('submit', function(event) {
  event.preventDefault();
  
  const $nameField = $('[name="name"]');
  const $ageField = $('[name="age"]');
  
  if (!$nameField.val() && !$nameField.val()) {
    alert('Inválido!');
    return;
  }
  
  alert('Válido!');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input type="text" name="name" placeholder="Nome" />
  <input type="text" name="age" placeholder="Idade" />
  <br />
  <button type="submit">Enviar</button>
</form>

This happens because empty strings ('') are considered values falsey in Javascript.

Browser other questions tagged

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