Restrict Acrobat PDF Form Fields to Numbers Only
Question details
The user needs to restrict a PDF form field so that only digits can be entered, intentionally blocking minus signs, decimals, or other separators.

- Product
- Adobe Acrobat XI
- Device & OS
- not provided
- Scenario
- Creating or modifying an interactive PDF form where a specific text field requires strict numerical input.
- Observed behavior
- The user must apply a custom JavaScript validation script to the form field to intercept keystrokes and enforce the numbers-only rule.
Ensure you have access to a PDF editor that supports JavaScript validation and keystroke formatting in form field properties, such as Adobe Acrobat.
Apply Custom Keystroke Script for Number Restriction
Use a simple Regular Expression (Regex) in the field's Keystroke properties to allow only digits and prevent non-numeric characters.
By applying a custom JavaScript regex to the field's keystroke event, you can actively prevent users from typing invalid characters like decimals, hyphens, or alphabetical letters into the text box.
Open your PDF file in your PDF editor (e.g., Acrobat XI) and navigate to the 'Prepare Form' or 'Edit Form' mode to view all interactive fields.
Right-click the target text field you want to restrict and select 'Properties' from the context menu.
Go to the 'Format' tab. Select 'Custom' from the Select Format Category dropdown, then click 'Edit' next to the Custom Keystroke Script section. Paste the following script: event.rc = /^\d*$/.test(event.change);
Click 'OK' to close the JavaScript editor, then close the Properties window. Exit form editing mode to test the field by typing letters and numbers.

Manage and Edit PDF Forms Easily with WPS Office
WPS Office provides a powerful, built-in PDF editor that allows you to easily view, fill, and manage interactive PDF forms without needing heavy or expensive software.
- 1. Download WPS Office: Download and install the free WPS Office suite on your computer or mobile device.
- 2. Open Your PDF Form: Launch WPS Office and open your PDF file using the built-in WPS PDF tool.
- 3. Interact with the Form: Navigate to the 'Form' tab to seamlessly fill out data fields, select checkboxes, and interact with the document.
- 4. Save and Export: Once your form is filled, click 'Save' or export it securely to share with others.

Frequently Asked Questions
Why does the suggested JavaScript allow an empty field?
The asterisk (*) in the regular expression `^\d*$` stands for 'zero or more occurrences' of a digit. This naturally allows the field to remain empty without triggering a validation error, which is useful for optional fields.
Can I modify this script to allow decimal numbers?
Yes. To allow decimals, you need to adjust the regular expression to permit a single period alongside digits. However, regex for accurate decimals is more complex than the digits-only script, often requiring a pattern like `/^\d*\.?\d*$/`.
What if users bypass the restriction by pasting non-numeric text?
Keystroke scripts primarily intercept live typing. To strictly prevent invalid pasted data, you should also add a script in the 'Validate' tab that checks the final value of the field upon submission or when the field loses focus.




