PDF Editing Issues

How to Preserve Leading Zeros in Acrobat JavaScript PDF Form Fields

Rana GarciaRana Garcia Nov 3, 2026 869 views

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").

How to Preserve Leading Zeros in Acrobat JavaScript PDF Form Fields
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.
Before you start

Ensure your PDF editing software supports JavaScript execution for form fields and that you have access to the field properties menu.

Solution 1Recommended

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.

1
Open Field Properties

Open your PDF form in editing mode, right-click the target text field (e.g., "RELAY NUM"), and select Properties.

2
Navigate to the Validation Tab

Click on the Validate tab (or Calculate tab, depending on your workflow) and choose the option to run a custom JavaScript.

3
Retrieve the Field as a String

Enter the script to retrieve the value as text: var relay = this.getField("RELAY NUM").valueAsString;

4
Apply Regex Validation

Add a regular expression to check for exactly four digits: if (!/^\\d{4}$/.test(relay)) app.alert("Please enter a 4-digit number");

5
Save and Test

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.

Use valueAsString and Regular Expressions for Validation
Validation Complete: Using valueAsString ensures that inputs like '0001' are treated as exact text, completely bypassing the default number formatting that removes leading zeros.
Edit PDF Forms Efficiently

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. 1. Open Your PDF: Launch WPS Office and open your interactive PDF document.
  2. 2. Access Form Tools: Navigate to the Form tab on the top ribbon to view available editing options.
  3. 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.
Native support for viewing and filling interactive PDF formsFully compatible with standard Adobe PDF formatsLightweight, fast, and easy-to-navigate PDF editor
microsoft office alternative - wps office

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.