Calculation scripts in PDF forms run every time any field value changes. Validation scripts run when the value of the field containing the validation script changes. This is an important distinction. Think of a validation script as pushing results out from the field and a calculation script as pulling results in from other fields.
A field with a value obtained from a calculation script will always revert back to that value. For this reason, a field with a calculated value should be set to Read Only, because there is no point in allowing the user to enter a value, since it will immediately change back to the calculated value on a blur event (exiting the field). For example, supposed you have a field called Full Name, that has the following custom calculation script:
event.value = this.getField("First Name").value + " " +
this.getField("Last Name").value;
No matter what the user enters in the field, the value will always be the value of the First Name and Last Name fields separated by a space. Setting a field’s readonly property to true will prevent data entry and also skip the field when tabbing through the form. If field highlighting is on, the field will not be highlighted.
Validation Script
What if you wanted the Full Name field to be calculated as above, but also allow the user to change the calculated result? In this case you would use a custom validation script in both the First Name and Last Name fields. The script needs to be in both fields because, remember, a validation script only runs when the value of the field containing the script changes. The script needs to run when a change is made to the First Name field and it also needs to run when a change is made to the Last Name field.
The script is slightly different for each field because event replaces this.getField(…) for the field containing the script. Here’s the script for the First Name field:
this.getField("Full Name").value = event.value + " " +
this.getField("Last Name").value;
Here’s the script for the Last Name field:
In this case, any value can be entered into the Full Name field, either before or after the values for First Name and Last Name are entered. The value will change again if the values of First Name or Last Name change.
Methods for Other Field Types
Everything I have written here so far relates to text fields. There are other field types that also store values but have slightly different methods for calculation and validation. In my next post I will discuss these methods for the following field types:
Check Box
Radio Button
List Box
Dropdown (“combobox”)