12 Replies Latest reply on Nov 19, 2015 5:30 AM by indio06

# If/ then formula on javascript for form

Hi,

I was wondering if someone can help me, I have a PDF form which I have designed and made fillable, however I am struggling on the condition on the last 2 boxes:-

Scenario:-

Box A = 20

Box B = 30

Box C = 30

I need:- (Box A + Box B - Box C)   if Box C< (Box A+Box B), then put the answer in Box F

if Box C > (Box A+Box B), then put the answer in Box G

Box F---------------------                                                      Box G-------------------------------

I know how to add A and B - C, but not sure how add the condition.

Many Thanks

• ###### 1. Re: If/ then formula on javascript for form

In other words, if the result is positive it should be in Box F and if it's negative in Box G?

Then you can use this code for Box F:

```var a = +this.getField("Box A").value;
var b = +this.getField("Box B").value;
var c = +this.getField("Box C").value;
var total = (a+b)-c;
if (total>0) event.value = total
else event.value = "";
```

For Box G use the same code by replace (total>0) with (total<0) ...

• ###### 2. Re: If/ then formula on javascript for form

Wow, you are a genius, it worked, spectacular!!!!!!

Thanks very much

• ###### 3. Re: If/ then formula on javascript for form

1 little add on to the same formula, if there is no value in Box C, then it should give 0 or nothing

• ###### 4. Re: If/ then formula on javascript for form

Change the last three lines to:

if (c==0) event.value = "";

else {

var total = (a+b)-c;

if (total>0) event.value = total

else event.value = "";

}

• ###### 5. Re: If/ then formula on javascript for form

Thanks very much, worked a dream again!!! brilliant work

• ###### 6. Re: If/ then formula on javascript for form

Hi There,

I have tried to add in a another box to calculate and I'm not getting any calculating values,,, do you know why?

• ###### 7. Re: If/ then formula on javascript for form

Its really wierd as its not calculating on the previous calculations now- is there a setting i am  missing

• ###### 8. Re: If/ then formula on javascript for form

There's most likely an error in your code somewhere. Check the JS Console (Ctrl+J) for error messages.

• ###### 9. Re: If/ then formula on javascript for form

can i send you file to check

• ###### 10. Re: If/ then formula on javascript for form

The below is how it appears on Document editing section not the field properties where it says custom calculation

//<AcroForm>

//<ACRO_source>52:Calculate</ACRO_source>

//<ACRO_script>

/*********** belongs to: AcroForm:52:Calculate ***********/

var a = +this.getField("21").value;

var b = +this.getField("23").value;

var c = +this.getField("41").value;

var d = +this.getfield

("23a").value;

var total = (a+b+d)-c;

if (total<0) event.value = total

else event.value = "";

//</ACRO_script>

//</AcroForm>

I get the error as undefined

• ###### 11. Re: If/ then formula on javascript for form

Did you copy it with the XML tags from the Edit All JavaScripts window? If so, that's a huge mistake and you probably corrupted the file.

Go back to an earlier version and NEVER edit the code via that window.

In addition to that you have a couple of errors in these lines of code:

var d = +this.getfield

("23a").value;

It should be:

var d = +this.getField("23a").value;

• ###### 12. Re: If/ then formula on javascript for form

That explains it--- Perfect!!!!!!!!Thanks so much!