Another Method for Calculation vs Validation Scripts
I just learned something new that is very helpful.
At the beginning of my journey toward expertise in all things Adobe Acrobat and the JavaScript programming language that makes it hum, I answered questions on the Acrobat Users Forum and quickly became a top-ranked participant. The website changed the format and I got very busy with my work, so I stopped answer questions, taking several years off.
I recently started answering again. This keeps me sharp, helps me learn, and gives me fresh ideas for newsletter content.
I wrote in Calculation vs Validation Scripts and Calculation vs Validation Scripts II that calculation scripts run every time any field value changes, but validation scripts only run when the value of the field containing the script changes. This is an important distinction because if you want the user to able to change a calculated field value after it has been calculated, it will usually revert to the calculated value. For this reason, calculated fields should usually be set to Read Only, since there’s no point in allowing the user to enter text. Read Only fields are also skipped over when tabbing through fields.
Dropdown fields and List Box fields can contain export values that are different from their displays values. This makes validation scripts tricky for these fields. It’s easier to use a custom Keystroke script with the changeEx event.
Summarizing
To summarize, calculation scripts are used in the calculated field when you don’t want to give the user the ability to change the fields value manually. No matter what they enter, the value will revert to the calculated value.
If you want a field calculated, but also want to give the user the ability to change the value after the fact, you would use validation scripts in other fields that “push” the calculated value into the field. This is possible because these scripts only run when the values of the fields containing the scripts change.
Another Option
First, I believe in giving credit where credit is due so I’m going to give a shoutout to Nesa Nurani, a true master of her craft, who selflessly provides quality answers to questions on the Acrobat User forum. Due to her answers of late I discovered that the value of a calculated field can be changed manually and will not revert to the calculated value if you put conditions in your script so that it will only run if the values of the fields referenced in the calculation are changed. This is done by using the source event. According to the Acrobat JavaScript Reference the source event is:
“The Field object that triggered the calculation event. This object is usually different from the target of the event, which is the field that is being calculated.”
For example, the following script will set the value of the field in which it is contained to the values of two other fields separated by a space:
event.value=this.getField("First Name").value +" "+
this.getField("Last Name").value;
The fields value will always revert to the calculated value because this script runs every time any field value changes. So if the values of First Name and Last Name are John and Smith respectively, no matter what the user types into the field, the value will always be John Smith.
Setting Conditions with the Source Event
First, before running the calculation, test whether the fields contained in the calculation triggered the calculation by testing whether event.source returns true:
if(event.source)
Also, test whether the field names in the calculation triggered the calculation:
event.source.name=="First Name" || event.source.name=="Last Name"
Now put it all together and only run the calculation script if the conditions return true:
if(event.source && (event.source.name=="First Name" ||
event.source.name=="Last Name"))
{
event.value=this.getField("First Name").value +" "+
this.getField("Last Name").value;
}
With this calculation script, the value will only change if the First Name or Last Name field values change, allowing the user to manually change the value after it has been calculated.