Inconsistent performance of custom calculation script

Jul 10, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

108

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Inconsistent performance of custom calculation script

Jul 10, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

109

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 10, 2019 0
Adobe Community Professional ,
Jul 10, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Most Valuable Participant ,
Jul 10, 2019

Copy link to clipboard

Copied

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)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Adobe Community Professional ,
Jul 10, 2019

Copy link to clipboard

Copied

And what is -integer and +integer?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Most Valuable Participant ,
Jul 10, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Adobe Community Professional ,
Jul 10, 2019

Copy link to clipboard

Copied

It is not defined in Acrobat DC.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Most Valuable Participant ,
Jul 10, 2019

Copy link to clipboard

Copied

Sorry, I didn't see they used "integer" literally... I thought you meant something like this:

var a = 5;

-a;

Which returns -5. What they did will not work, of course.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
Jul 10, 2019

Copy link to clipboard

Copied

Thank you. I read a JS tutorial that did not apply here, obviously, when using "integer", which I achieved the same effect by doing this:

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

if (a <= 0) { var b = 'BBY' } else if (a > 0) { var b = 'ABY' }

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

The script is now working,  however I have stumbled on a new problem: no entry on the "age" field yields the "current" year: 35BBY

Is there a way to have no entry on the age field yield no result in the year script?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0
try67 LATEST
Most Valuable Participant ,
Jul 10, 2019

Copy link to clipboard

Copied

Yes, you can do it like this:

if (this.getField("age").valueAsString=="") event.value = "";

else {
     // place rest of your code here

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 10, 2019 0