How to Preserve Leading Zeros in Acrobat JavaScript PDF Form Fields
Question details
The user needs to validate a four-digit PDF form field using JavaScript without losing leading zeros (e.g., preserving "0000" or "0001").

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating or editing interactive PDF forms that require a strict four-digit input with custom JavaScript validation.
- Observed behavior
- When retrieving numeric input, the application may drop leading zeros. The goal is to retain the exact string of four characters entered by the user.
Ensure your PDF editing software supports JavaScript execution for form fields and that you have access to the field properties menu.
Use valueAsString and Regular Expressions for Validation
Read the form field as a text string instead of a numeric value to prevent the software from automatically stripping leading zeros.
By default, PDF scripts may interpret numerical inputs strictly as numbers, mathematically removing any leading zeros. Using the valueAsString property forces the application to treat the input as text, preserving every character exactly as typed.
Combining this with a regular expression allows you to enforce strict character limits, such as requiring exactly four digits.
Open your PDF form in editing mode, right-click the target text field (e.g., "RELAY NUM"), and select Properties.
Click on the Validate tab (or Calculate tab, depending on your workflow) and choose the option to run a custom JavaScript.
Enter the script to retrieve the value as text: var relay = this.getField("RELAY NUM").valueAsString;
Add a regular expression to check for exactly four digits: if (!/^\\d{4}$/.test(relay)) app.alert("Please enter a 4-digit number");
Save your script, close the properties window, and test the form by entering a value like "0005" to ensure the zeros are preserved and no alert is triggered.

Manage and Edit PDF Forms with WPS Office
WPS PDF provides an intuitive interface for managing, filling, and editing interactive PDF forms. Whether you are handling text fields, checkboxes, or numeric data, WPS makes document management hassle-free.
- 1. Open Your PDF: Launch WPS Office and open your interactive PDF document.
- 2. Access Form Tools: Navigate to the Form tab on the top ribbon to view available editing options.
- 3. Fill or Edit Fields: Click directly on text fields to fill them out, or use the editing tools to modify field properties and adjust formatting.

Frequently Asked Questions
Why do leading zeros disappear in PDF form fields?
If a form field or its underlying script evaluates the input as a mathematical number, it automatically drops leading zeros because they hold no mathematical value. Reading the input explicitly as a text string prevents this behavior.
What does valueAsString do in PDF JavaScript?
The valueAsString property forces the PDF software to retrieve the exact characters entered by the user as a pure text string, bypassing any automatic numeric formatting or calculation.
How do I ensure exactly four digits are entered in a PDF form?
You can use a regular expression like /^\d{4}$/ within a custom JavaScript validation script. This restricts the acceptable input strictly to four numeric characters, triggering an error if the user enters more or fewer digits.




