1

I've five variables in a form, for simplicity I named them A, B, C, D and E. In order to submit the form (A or (B or C)) and (D or E) must have a value (I hope it's clear :P). I've tried with

if ((A == "" || (B == "" || C == "")) && (D == "" || E == "")) {
    alert ("Error!");
} else {
    form.submit();
}

But it doesn't work in every case. For example, if A, B and C have a value, the form is submitted even if neither D or E have values. Thank you for the help!

3
  • you are missing a { Commented Aug 22, 2017 at 18:34
  • 1
    I am thinking your or logic is wrong.... Thinking you want some ANDs and not ORs. Commented Aug 22, 2017 at 18:35
  • If you're using this code to validate a form you should consider trimming the string value and then checking if the length is greater than 0 (or compare it against an empty string like you're doing). This way a user can't enter a bunch of blank spaces to circumvent your validation. Unless you're already formatting the values before you store them in A, B, C, D, E. Commented Aug 22, 2017 at 23:55

4 Answers 4

2

If you have all strings, you could just reverse the condition and check all with logical AND and one OR.

var a = '', b = '', c = '', d = '', e = '';
    
if (a && b && c || d && e) {
    console.log('form.submit();');
} else {
    console.log('Error!');
}

Why it works:

given

(A == "" || (B == "" || C == "")) && (D == "" || E == "")

simplified A == "" is equivalent to !A

(A == "" || B == "" || C == "") && (D == "" || E == "")

(!A || !B || !C) && (!D || !E)

De Morgan's laws !(a && b) = !a || !b or !(a || b) = !a && !b

 (!A || !B || !C) && (!D || !E)

 !(A &&  B &&  C) && !(D &&  E)

!((A &&  B &&  C) ||  (D &&  E))

operator precedence

!(A && B && C || D && E)

now switch/negate then <-> else

A && B && C || D && E
Sign up to request clarification or add additional context in comments.

3 Comments

That's the not the logic that the OP has provided.
actually its the same logic, just negated.
So you don't need the parenthesis with AND in this case?
1
if (A == "" || B == "" || C == "" && (D == "" || E == "")) {
        alert ("Error!");
} else {
       form.submit();
}

Comments

1

You need to change your logic to:

if ((A != "" || (B != "" || C != ""))
    && (D != "" || E != "")) {
        form.submit();
} else {
       alert ("Error!");
}

The alternative is to do it like so:

if ((A == "" && (B == "" && C == ""))
    || (D == "" && E == "")) {
        alert ("Error!");
} else {
      form.submit();
}

Edit: the first snippet can be refactored to the following:

if ((A || (B || C )) && (D || E )) {
        form.submit();
} else {
       alert ("Error!");
}

And the same can be done with the second snippet with the NOT operator !.

3 Comments

alert ("Error!");
The only thing I'd add would be instead of using the == operator use the === operator, this checks value and type, unless type doesn't matter. Another discussion, but should be considered.
@DylanWright, the variables are guaranteed to be strings, since they are input values.
0

I updated the code a bit. I think this should work for you.

function isNonEmpty(string) {
  //returning the string's truthiness value
  return string.trim() ? true : false;
}

//change these values around to make sure the logic is correct
var A = "cat",
  B = "    ",
  C = "dog",
  D = "",
  E = " turtle";

//almost exactly what you have just calling a function for neatness
if ((isNonEmpty(A) || isNonEmpty(B) || isNonEmpty(C)) &&
  (isNonEmpty(D) || isNonEmpty(E))) {
  console.log("Valid form. Time to submit!");
} else {
  console.log("Invalid form. Please check your values!");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.