Entering scripts for form fields

Learn about two types of form-field scripts: scripts associated with actions and scripts associated with the value of a field.

By July 7, 2006

 

Scope: Acrobat Professional 6.0 and greater
Skill Level: Beginner
Prerequisites: Familiarity with Acrobat Professional

Form-field scripts can be divided into two areas: scripts associated with actions performed on the instance of a field on a page (also called Widget scripts — a specific instance of a field is called a Widget), and scripts associated with the value of a field.

The Widget scripts are for the mouse and field-focus actions performed by the user. The field-value scripts are related to internal actions Acrobat takes when a field value changes. The different types of scripts in each area are listed below.

Widget scripts:

“Mouse Enter” Runs when cursor enters field
“Mouse Exit” Runs when cursor exits field
“Mouse Up” Runs when cursor is over field and mouse button is pushed
“Mouse Down” Runs when cursor is over field and mouse button is released
“On Focus” Runs when field gains keyboard focus
“On Blur” Runs when field loses keyboard focus

Field-value scripts:

“Calculate” Runs when any field value on the document changes
“Keystroke” Runs for each keystroke entered into a text field
“Validate” Runs when the associated field value changes
“Format” Runs after the Validate script, but only if Validate returns true

All these scripts are accessed from the Field Properties dialog.

To display the dialog:

1. Activate the Select Object Tool on the Advanced Editing toolbar, Figure 1.

Figure 1: Activating the “Select Object Tool”
from the Acrobat “Advanced Editing” toolbar

2. Right click on the form field of interest. This action displays a context-popup menu.

3. Select Properties… from the menu. This action activates the Field Properties dialog, Figure 2.

Figure 2 – MouseUp Script set for a text field

Widget scripts

All field types have an Actions tab on their Properties dialog. This tab provides access to the Widget scripts. The “Select Trigger” pull-down list selects the action that triggers the script, MouseUp, MouseDown and so on. The “Select Action” pull-down list selects the type of action to be performed. For this tip we are only concerned with the “Run a JavaScript” action.

To add a new JavaScript action:

  1. Select the desired trigger from the “Select Trigger” pull-down list.
  2. Select “Run a JavaScript” from the “Select Action” pull-down list.
  3. Press the Add button. This action will display the script editor.

If the field has existing actions, they will be displayed in the Actions window. Figure 2 (above) shows the Properties dialog for a text field with a previously defined “MouseUp” script.

To edit an existing script:

  1. Select the script from the Actions window.
  2. Press the Edit button.

Field-value scripts

Separate tabs are provided on the Field Properties dialog for each of the field-value scripts. Since different types of fields have different types of values, these tabs are different for each field type. For example, the button field does not have any field value scripts since it doesn’t have a value, while the text field uses all the different field scripts. The table below lists the field-value scripts used by each field type.

There are several cases where Acrobat does not provide a user interface for accessing a particular script, yet the field will still run the script if it exists (marked as “No UI”). Acrobat does not provide a user interface because these scripts are not used with the particular field type under normal circumstances. However, this doesn’t mean that these scripts could not be useful, so ambitious users can still set the scripts using a JavaScript function.

Field Type

Format

Keystroke

Validate

Calculate

Button

X

X

X

X

Text

Yes

Yes

Yes

Yes

Check Box

X

No UI (1)

No UI (1)

No UI(1)

Radio Button

X

No UI (1)

No UI (1)

No UI (1)

Combo Box

Yes

Yes

Yes

Yes

List

X

Yes(2)

No UI (1)

No UI (1)

Signature

Yes(3)

No UI (1)

No UI (1)

No UI (1)

(1) Acrobat does not provide a user interface to these scripts, but they can be set from the Console Window with the setAction() JavaScript function.

(2) In the List Field the Keystroke script is called the Select Change script.

(3) In the Signature Field the Format script is called the Signed script.

The text field uses all four of the field-value scripts, so we’ll use it as the example in the following discussion.

Format and Keystroke scripts: (both are on the same tab)

In the text field, the Format and Keystroke scripts are used together to enforce formatting. The Keystroke script ensures that only certain keys are entered and the format script adds the final touches when field data is committed.

To access:

1. Open the Field Properties dialog.

2. Select the Format tab.

3. On Select format category, choose “Custom.” This action displays the Format and Keystroke scripts (Figure 3).

4. Push the edit button for either Format or Keystroke to create a new script or edit an existing one.

Figure 3 – Entering the Format and Keystroke scripts

Validate script:

The Validate script is run when a field value is committed. When users press the Enter key or tab out of the field, they are indicating the entered data should be put into the field value, called “committing” the value. The Validate script sits between the user action that signals the value should be committed (tabbing, pressing Enter or selecting another field) and the actual value being committed.

To access:

  1. Open the Field Properties dialog.
  2. Select the Validate tab.
  3. Select the Run custom validation script radio button (Figure 4). Be careful with these radio buttons. When another option is selected, the existing validation script is lost.
  4. Push the edit button to create a new script or edit an existing one.

Figure 4 – Entering a Validation script

Calculate script:

Calculate scripts are run when any field value on the document is changed.

To access:

  1. Open the Field Properties dialog.
  2. Select the Calculate tab.
  3. Select the Custom calculation script radio button (Figure 5). Be careful with these radio buttons. When another option is selected the existing validation script is lost.
  4. Push the edit button to create a new script or edit an existing one.

Figure 5 – Entering a Calculation script


Did you know?

  • You can ask a question and get an answer from one of our experts.
  • You can search our database of over 800 tutorials by product and/or topic.
  • You can leave a comment below for the author of this tutorial.

Related topics:

PDF Forms, JavaScript

Top Searches:

Convert existing forms to fillable PDFs, fill and sign any form, Edit PDF, create PDF, Action Wizard

2 comments

Comments for this tutorial are now closed.

Lori Kassuba

5, 2015-03-12 12, 2015

Hi michaelA,

Can you post your question here and be sure to select the JavaScript category so our experts can help you out?
https://answers.acrobatusers.com/Questions.aspx?listType=needsAnswers

Thanks,
Lori

michaelA

5, 2015-03-10 10, 2015

I have a PDF form with 3 field A,B Combo Boxed and C ,Text field. I require the background property of field C to change depending on the
selected value of Fields A and B combined ...How do you do it? I am not a scripting guru. Any help would be appriciated.

Thanks
Michael

Lori Kassuba

12, 2015-01-29 29, 2015

Hi Stephanie Taylor,

Can you post your question here and be sure to select the JavaScript category so our JavaScript experts can assist you?
https://answers.acrobatusers.com/AskQuestion.aspx

Thanks,
Lori

Stephanie Taylor

3, 2015-01-26 26, 2015

I am currently creating a fillable form for travel expenses and part of the form originally used an Excel spreadsheet to complete the calculations on the number of meals at a certain price point to give the final meal per diem. Can I do this in Adobe? I am currently using Acrobat Pro 9 and LiveCycle Designer ES 8.2

Thom Parker

9, 2014-12-01 01, 2014

Melissa,  Please see this article and especially the sample files
https://acrobatusers.com/tutorials/date_time_part2

Melissa

2, 2014-11-24 24, 2014

I am trying to create a script that takes the birthday in one column and calculates the age in the next.  I have the birthday column formatted as dd/mm/yyyy.  Is there an easy way to do this?

Thom Parker

1, 2014-09-15 15, 2014

Have you seen this article? It has the script you want in it.
http://acrobatusers.com/tutorials/formatting_text_fields

However, your question isn’t very clear. If you mean you want to automatically apply the script to all the fields then you’ll need to use the “field.addScript()” function. The trick for dealing with any kind of table scripting is to have named your fields in a regular way so that you can loop over them in an organized way.

Warrick

6, 2014-09-15 15, 2014

Hi Guys,

How do I add a convert to uppercase script to all text fields in a table in a form in acrobat PRO XI as the user enters data?

To avoid having to manually add the script to each text field.

Thanks!
Warrick

 

Thom Parker

1, 2014-09-12 12, 2014

Michael,  The formatting and validation functions are most likely to be defined in a Document Script.  Assuming you are on Acrobat X or XI, look on the “Tools > JavaScript” Panel for the Document JavaScripts option.

Michael

4, 2014-09-10 10, 2014

I am not a code writer so this question may be ridiculous.

I am using an existing form to create a new form.  The existing form uses scripts to validate some entries and format dates.  The scripts are simply names like CS_PDF_V and I have no idea what the code is for the script.  I think it may have been set up as a variable so the variable could be referenced in the formatting dialog box, but not allow someone to alter the actual code.

The original form only allowed 60 characters in a field, but I want to change that to 80.  If I change it in the “Text Field Properties” window the “options” tab it allows me to type more information, but when I hit enter, or tab I get an alert box saying only 60 characters are permitted in this field.  in the filed properties box on the validate tab “Run custom validation script:” I see “CS_PDF_V( event );” [without the quotes].  This makes me think the code limiting the field to only 60 characters is in a variable called “CS_PDF_V”.  I can’t seem to be able to find where that variable is defined.  I’ve opened the JavaScript console, but I still do not see the code.  It must be in the somewhere.

Any help with this issue is greatly appreciated.

Lori Kassuba

9, 2014-02-24 24, 2014

Hi toni,

Let’s know if this tutorial helps:
http://acrobatusers.com/tutorials/how-to-add-a-date-stamp-to-a-form

Thanks,
Lori

toni

11, 2014-02-18 18, 2014

how do you insert date on form.  adobe acrobat 10.0 will not insert date. please help

Comments for this tutorial are now closed.