Skip to main content
Participant
July 10, 2019
Question

Inconsistent performance of custom calculation script

  • July 10, 2019
  • 3 replies
  • 648 views

I am new to both PDF form and JavaScript, so I apologize if this question seems obvious, or isn't worded effectively.

I'm attempting to create a custom text field which will calculate the user's "Star Wars Galactic Birth Year" as part of a PDF form. Basically, type in your age, and it will spit out your "birth year", appended with BBY or ABY depending on if the number is positive or negative. (35ABY acting as the "current" year, in this example.) Here's the custom script I cobbled together. ("Age" is the name of the field, as I'm sure you could surmise.)

event.value = Math.abs(a) + b

var a = (35 - this.getField("Age").value)

if (a = -integer) { var b = 'BBY' } else if (a = +integer) { var b = 'ABY' } else if (a = 0) { var b = 'BBY' }

I've never used JavaScript before, so I assume am making some kind of very basic mistake. The calculation doesn't run consistently, and it never seems to allow the text field to be left empty. The math seems to get wonkier with each attempt. Is there a way to have it "reset" with each input change?

Any help would be appreciated. I'm way out of my depth.

This topic has been closed for replies.

3 replies

Bernd Alheit
Community Expert
Community Expert
July 10, 2019

And what is -integer and +integer?

try67
Community Expert
Community Expert
July 10, 2019

It's the same as mulitplying it by -1 and +1, resp.

Bernd Alheit
Community Expert
Community Expert
July 10, 2019

It is not defined in Acrobat DC.

try67
Community Expert
Community Expert
July 10, 2019

In addition to Bernd's correct remark about the order of your lines, you've also made a classic error in your code, ie you've used the value assignment operator ( = ) where the value comparison operator ( == ) should be used.

For example, this:

if (a = 0)

Should be:

if (a == 0)

Bernd Alheit
Community Expert
Community Expert
July 10, 2019

Setting event.value at the beginning makes no sense. Acrobat executes the code from the top to the bottom, not from bottom to top.