Skip to main content
Inspiring
June 2, 2017
Answered

Javascript 'if... else' script for field in fillable pdf forms Acrobat DC

  • June 2, 2017
  • 4 replies
  • 6851 views

I am hoping somone might be able to help with a javascript calculation for an order form.

I would like my script checking because it doesn't work!

I have a 2 fields :  a Subtotal field, a Freight Charge field.

I have written a script for my freight charge field where if the subtotal field amount is less than 360.00 then the freight charge must be 9.00. If more than 360.00 then the freight charge should be 0.00

var "SUBTOTAL" = Number (this.getField ("SUBTOTAL").value);

if ("SUBTOTAL" < 360.00)

{

    event.value = 9.00

} else {

    event.value = 0.00

}

Also, is there a way of making the 9.00 the visible default value until the Subtotal reaches 360 ??

If anyone can help then huge thanks in advance!

This topic has been closed for replies.
Correct answer try67

Use this code:


var subtotal = Number (this.getField ("SUBTOTAL").value);

if (subtotal< 360.00)

{

    event.value = 9.00

} else {

    event.value = 0.00

}

4 replies

Inspiring
June 6, 2017

tTanks Try67.

In fact after a little thought I realised that the 20% needs to be applied to each field - separately - as follows:

(SUBTOTAL*0.20)+(FREIGHT*0.20)

The whole calculation now works!

Many thanks.

Bernd Alheit
Community Expert
Community Expert
June 6, 2017

This will work also:

(SUBTOTAL+FREIGHT)*0.20

Inspiring
June 5, 2017

Many thanks try67

It worked BUT is it possible that this script is affecting another field from operating properly.

The subtotal field works fine

The freight field works fine (thanks to your script)

But the tax field will only calculate 20% on the freight and does not calculate 20% of the subtotal. I have tried using both the ‘Simplified Field Notation’ section and the ‘Custom Calculation Script’ and neither will also add in the 20% tax on the subtotal.

I have tried various permutations including the following:

SUBTOTAL+FREIGHT*0.20

("SUBTOTAL"+"FREIGHT")*0.20

Many thanks in advance if you can help!

try67
Community Expert
Community Expert
June 5, 2017

There's no reason it should affect another field. The first notation is used is the correct one. Check the JS Console for error messages, and also the fields calculation order. You need to make sure that this field is calculated after SUBTOTAL for it to work correctly.

Inspiring
June 4, 2017

Thanks Bernd but still not working.

I've tried all permutations of "subtotal" or just subtotal but I keep getting "Syntax Error missing variable name 1:at line 2"

Thanks if you have any other suggestions?

thanks in advance

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 4, 2017

Use this code:


var subtotal = Number (this.getField ("SUBTOTAL").value);

if (subtotal< 360.00)

{

    event.value = 9.00

} else {

    event.value = 0.00

}

Bernd Alheit
Community Expert
Community Expert
June 2, 2017

Use subtotal not "SUBTOTAL" for the variable.