Format text box using JavaScript

The basic format settings are usually enough for most cases. If you want to have more advanced control of format, you can use JavaScript.

Note: Using keystroke and format JavaScript will override format settings.

The information about JavaScript functions that are used to format form fields can be found at Acrobat Forms API Reference. The functions we are particularly interested in are AFDate_KeystrokeEx, AFDate_FormatEx, AFTime_Keystroke, AFTime_Format, AFPercent_Keystroke, AFPercent_Format, AFSpecial_Keystroke, AFSpecial_Format, AFNumber_Format, AFNumber_Keystroke. You can search for those names in the reference to get detailed information.

The above functions with Format suffix should be put in the Format trigger. When a PDF viewer displays the contents of a text box, it calls Format trigger before showing it. The functions with Keystroke suffix, should be put in the Keystroke trigger. Any time user types in the text box, the Keystroke trigger will be called to validate the input.

Here is the list of frequently used formats:

Description Example Javascript
Date 01/31/2008 AFDate_FormatEx("mm/dd/yyyy");
Date 1/31/2008 AFDate_FormatEx("m/d/yyyy");
Date 1/31/08 AFDate_FormatEx("m/d/yy");
Zip code 12345 AFSpecial_Keystroke(0);
Zip+4 12345-1234 AFSpecial_Keystroke(1);
Phone number (123) 456-7890 AFSpecial_Keystroke(2);
Social Security Number 123-45-6789 AFSpecial_Keystroke(3);
Money (minus sign if negative) $12,345.00
AFNumber_Keystroke(2, 0, 0, 0, "$", true);
AFNumber_Format(2, 0, 0, 0, "$", true);
Money (parentheses if negative) $12,345.00
AFNumber_Keystroke(2, 0, 2, 0, "$", true);
AFNumber_Format(2, 0, 2, 0, "$", true);

This tutorial demonstrates how to create a text box and display its contents in phone number format using AFSpecial_Format. Demo files can be downloaded here: PhoneNumber.doc and PhoneNumber.pdf.

  1. Launch Microsoft Word. Choose Add Text Box from CenoPDFmenu. In the Text Box Properties dialog box, click Format and Validate tab. Click the Format trigger. Type


    in the JavaScript window. Click the OK button.

    Add a PDF format and validate trigger

  2. Export to PDF file and open it in Adobe Reader. Type a phone number in the text box and press Enter. Note the phone number is displayed in correct format.

    Open the PDF file with phone number box in Adobe Reader