Using Acrobat JavaScript in Forms
Forms Essentials
6
Validation Options
You may use validation to enforce valid ranges, values, or characters entered in form fields.
The main reason to use validation is to ensure that users are only permitted to enter valid
data into a form field.
To apply validation to a field action, invoke the field’s
setAction
method, and pass
"Validate"
as the first parameter and a script containing a call to
AFRange_Validate
as the second parameter. The function
AFRange_Validate
is
located in
Javascripts\aform.js
, and requires 4 parameters:
bGreaterThan
(boolean
value),
nGreaterThan
(first value in the range),
bLessThan
(boolean value), and
nLessThan
(last value in the range).
For example, the following code ensures that all values entered in the form field are from 0
to 100, inclusive:
f.setAction(
"Validate",
‘AFRange_Validate(true, 0, true, 100)’
);
Calculation Options
Calculation options make it possible to automate mathematical calculations associated
with form fields. To apply a calculation to a form field action, invoke the form field’s
setAction
method, pass
"Calculate"
as the first parameter, and pass a script
containing a call to a calculation script as the second parameter.
If you would like to perform simple calculations on an array of form field values, you may
use a convenient script already written for you called
AFSimple_Calculate
in the
second parameter. The function
AFSimple_Calculate
is located in
Javascripts\aform.js
, and requires 2 parameters:
cFunction
(the type of calculation
to be performed) and
cFields
(a field list that may be separated by commas or spaces, or
may be an array). The first parameter specifies the type of calculation, which may be a sum
(
"SUM"
), product (
"PRD"
), average (
"AVG"
), minimum (
"MIN"
), or maximum (
"MAX"
).
For example, the following code calculates the sum of the values entered into two text
areas on a form:
var arr = new Array("line.1", "line.2");
f.setAction(
"Calculate",
‘AFSimple_Calculate("SUM",arr)’
);
Acrobat JavaScript Scripting Guide
101