7 Replies Latest reply on May 7, 2014 4:50 PM by gkaiseril

# Only calculate if "Variable" is present

Like many on this forum, I just started playing with javascript to make our forms smarter. I am dealing with a not so simple equation where there is only one field of input and the rest of the equation is written in. Here is my equation:

"DraftTempPost" is the only input box.

(DraftTempPost/40)-2.75

The problem I am having is that when the field "DraftTempPost" is blank, the field where my equation is located it shows  -2.75.

How can I write a script that will only run the calculation when "DraftTempPost" has a value that is greater than 1?
Thank you for all of your help.
• ###### 1. Re: Only calculate if "Variable" is present

What should happen if the value of the field is neither blank, nor bigger than 1 (for example, 0.5)?

• ###### 2. Re: Only calculate if "Variable" is present

I didn't think about that but that but yes it would have to calculate anything that is not zero or blank.

• ###### 3. Re: Only calculate if "Variable" is present

You can use something like this:

var v = this.getField("DraftTempPost").value;

if (v=="") event.value = "";

else event.value = (Number(v)/40)-2.75;

• ###### 4. Re: Only calculate if "Variable" is present

I know i am missing some language to go along with your script (thank you btw for taking the time with this). I pasted your script and it does nothing, nor does it give me any errors.

Should there be language behind it, something like this?

Function (((....

than the equation?

• ###### 5. Re: Only calculate if "Variable" is present

Open the JS Console (Ctrl+J) and check for errors. What exactly happens when you change the value of the DraftTempPost field?

• ###### 6. Re: Only calculate if "Variable" is present

I have been staring at a computer for far to long and failed to see that the input "DraftTempPost" needed to be changed to "DraftTempPre"   It was all operator error on my part.

thank you very much for your help.

• ###### 7. Re: Only calculate if "Variable" is present

I assume you are placing this script in the field where the result of the calculation is to appear.

Sine you want to control the display, you need to use a conditional statement and that can only be done in the Custom JavaScript calculation option. You will need to write the JavaScript code and link to the field and event objects.

event.value = (this.getField("DraftTempPost").value / 40) - 2.75;