Skip to main content
August 25, 2022
Question

Javascript with if then with an and/or

  • August 25, 2022
  • 1 reply
  • 912 views

Hello,

 

I'm working on an error check that looking at "S3 Breakfast meal count" and if it is greater than 0 then I need it to look at the other two and if either one of those is greater than 0 (or both are) its okay and doesn’t return my error code.  However, if S3 Breakfast Meal count is greater than 0 and the other two are not greater than 0 two I need my error to return.

Here’s my code to date…

 

if (this.getField("*S3 B Meal Count").value >0 && this.getField (("S3 Add <= Count").value >0) or (this.getField ("S3 Add > Count").value >0)) event.value = "";

else event.value = "Add meal count for additional amount for lunch.";

 

Part of the above works…

               

For the or I’ve tried && and ||.  The || is only looking at the last half if its true it removes my code.  I tried doing a var script, but I couldn’t work that one out in my head. 

 

And yes, I know my field names stink, but I didn’t know anything about JavaScript when I developed my field name schema for this form (two years ago).  

 

I think I'm close with my code.

 

Any help is apprecaited.  

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
Community Expert
August 25, 2022

Try this:

if (this.getField("*S3 B Meal Count").value >0 && (this.getField ("S3 Add <= Count").value >0 || this.getField ("S3 Add > Count").value >0)) event.value = "";

else event.value = "Add meal count for additional amount for lunch.";

August 26, 2022

thanks...I'm closer...the issue now is that the else is appear if "*S3 B Meal Count" is 0. 

if (this.getField ("*S3 B Meal Count").value > 0 && (this.getField ("S3 Add <= Count").value >0 || this.getField ("S3 Add > Count").value >0)) event.value = ""; 
else event.value = "Add meal count for additional amount for lunch.";

 Would a var solve the problem...

var EC10 = this.getField ("*S3 B Meal Count").value;
if EC10 > 0 && (this.getField ("S3 Add <= Count").value >0 || this.getField ("S3 Add > Count").value >0)) event.value = ""; 
else event.value = "Add meal count for additional amount for lunch.";

But there is an error in that...

 

Again, I'm learning by trial and error. 

Bernd Alheit
Community Expert
Community Expert
August 26, 2022

May be that "*S3 B Meal Count" is not exact 0.