Skip to main content
Participant
March 3, 2017
Question

Javascript Calculation

  • March 3, 2017
  • 1 reply
  • 1142 views

Hi All,

Using Adobe Acrobat X Pro

I don't have any programming or Javascript experience so any help will be greatly appreciated.

I am trying to figure out how to do a If/Else calculation in a Adobe form.

In the field box named (S4_SU_PS) if I enter any value in that field I want it to calculate the difference.  For example I want it to ignore any value up to >£5.75. So if I was to enter £17 in that box then I would like it to return the difference of £11.25 in that same field.  If a value of lets say a lower amount is entered £4.35 then I would like that to be disregarded. So any thing below <5.75 shows up as £0.00.Otherwise when I come to adding up the value in this filed it will be incorrect as I am only interested in adding anything above the £5.75 if that makes sense.

Sorry if haven't been clear in explaining in what I am trying to achieve.

Many thanks in advance.

Regards

John

This topic has been closed for replies.

1 reply

Karl Heinz  Kremer
Community Expert
Community Expert
March 3, 2017

The following script should do what you want to do:

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

if (v <= 5.75) {

    event.value = 0;

}

else {

    event.value = v - 5.75;

}

Use this as the custom calculation script for your second field. Keep in mind that all formatting (e.g. two decimals, currency symbol, ...) are handles via the formatting options.

You may want to review the syntax rules for JavaScript. The basic language is not too complex, and that will help you to understand how e.g. an if/else construct works. See here for example for a simple way to get into JavaScript programming for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

Participant
March 6, 2017

Hi Karl,

Thank you so much, this is absolutely Brilliant!

The script you have provided me with does exactly what I wanted it to do.

The only problem I have got now is if I enter any figure/number in the field S4_SU_PS my Total_Sum field which adds up the values in each field does not add the output calculation value. For some reason it remembers the input value that gets entered in the S4_SU_PS field and adds that up instead.

Example

I enter £10 in the S4_SU_PS field, it does the correct calculation and returns the value in that field as £4.30 (10 - 5.70).

My Total_Sum field which adds all the values in each fields does not add the £4.30 rather it will add the inputted £10 value. I can't figure out why it is doing this.

Once again your help will be greatly appreciated.

Many thanks

Regards

John

Karl Heinz  Kremer
Community Expert
Community Expert
March 6, 2017

This is usually an indication of a problem with your calculation order: The Total_Sum field is calculated before the field value depending on S4_SU_PS gets calculated. You can change the calculation order in Acrobat X by selecting the following while in the form editor:

Other Tasks>Edit Fields>Set Field Calculation Order...