Inconsistent performance of custom calculation script
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.
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.
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)
Copy link to clipboard
Copied
And what is -integer and +integer?
Copy link to clipboard
Copied
It's the same as mulitplying it by -1 and +1, resp.
Copy link to clipboard
Copied
It is not defined in Acrobat DC.
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.
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?
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
}

