0

I have an IF statement in order to validate some things:

-If the input value is different from either 000000000000000000 (eighthteen zeroes) or 0 (one zero).

-if all the input has the same numbers

-if all the numbers in the input are only 0

Code:

else if((IDProdAIngresar!='000000000000000000'||IDProdAIngresar!='0') && (allEqual(CantidadIngresadaSinPunto)) && CantidadIngresadaSinPunto.indexOf('0') > -1)
{
  $("#MSJ").html("Error: La cantidad ingresada no puede ser cero");
  $("#ModalMSJ").modal("show");
}

The problem is when the input is either 000000000000000000 (eighthteen zeroes) or 0 (one zero) the error keep showing when it shouldn't.

As I understand,if it's an AND all the validations has to be met for the error to show. However I have no idea why the error keep showing even when the (IDProdAIngresar!='000000000000000000'||IDProdAIngresar!='0') is not met and the other validations are, as shown in the image bellow:

Screenshot

Thanks in advance!

Roly
  • 161
  • 1
  • 5
  • 18
  • 3
    You have a logic error: `x != a || x != b` is always true, since `x` is always either not `a` or not `b`. You need `&&` instead. –  Oct 21 '19 at 17:22
  • 1
    "*f the input value is different from either 000000000000000000 (eighthteen zeroes) or 0 (one zero).*" an input of `"0"` (single zero) will return `true` here because it's not eighteen zeroes. And if you get `"000000000000000000"` that's not `"0"` so, `true` again – VLAZ Oct 21 '19 at 17:23
  • @ChrisG What should I do then? I cannot Use `&&` because then the error would never be shown? – Roly Oct 21 '19 at 17:27
  • 1
    @Roly _“because then the error would never be shown”_ — Why do you think that? What do you think does `a !== "1" && a !== "2"` return if `a = "3"`? – Sebastian Simon Oct 21 '19 at 17:31
  • Can you please clarify which inputs are valid and which inputs are invalid? –  Oct 21 '19 at 17:32
  • 1
    Do you want `IDProdAIngresar` to *never* be either zero or eighteen zeroes? Then do `IDProdAIngresar == '000000000000000000' || IDProdAIngresar == '0'`. If you want `IDProdAIngresar` to *only* be one or the other and throw an error otherwise, then `IDProdAIngresar != '000000000000000000' && IDProdAIngresar != '0'` (or the equivalent but harder to read IMO `!(IDProdAIngresar == '000000000000000000' || IDProdAIngresar == '0')`). – VLAZ Oct 21 '19 at 17:36
  • @VLAZ thanks, I used the last you suggested and it worked. – Roly Oct 21 '19 at 17:49

0 Answers0