How to Reject Non-Numeric Input in PDF Forms Using JavaScript
Question details
The user needs to restrict a fillable PDF form field so that it only accepts numeric characters and alerts the user if non-numeric data is entered.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating or editing a fillable PDF form where strict numerical data entry is required.
- Observed behavior
- The goal is to display a pop-up warning and reject the keystroke input when a user attempts to type non-numeric characters into a specific text field.
Ensure you are using a PDF editor that supports JavaScript execution and allows you to modify advanced form field properties.
Restrict PDF Form Field to Numbers Using a Keystroke Script
You can enforce numeric-only input by adding a custom JavaScript to the text field's Keystroke event.
By utilizing a simple regular expression inside a custom keystroke script, you can intercept invalid inputs immediately as the user types, preventing letters and symbols from being entered into a numeric field.
Open your PDF form in edit mode. Right-click the text field you want to modify and select 'Properties' from the context menu.
Navigate to the 'Format' or 'Actions' tab (depending on your specific PDF editor) and find the 'Custom Keystroke Script' or 'Run a JavaScript' trigger.
Enter the following code into the script editor window: if (/\D/.test(event.value)) { app.alert("Only numbers are allowed!"); event.rc = false; }
Save your script and close the properties window. Exit form-editing mode and test the form by attempting to type a letter into the field to ensure the alert triggers successfully.
Build and Manage Fillable PDF Forms with WPS Office
WPS Office provides a powerful built-in PDF editor that allows you to easily create fillable forms, set field properties, and manage data validation directly alongside your daily document tasks.
- 1. Open Your PDF: Launch WPS Office and open your PDF document containing the form.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all interactive form creation tools.
- 3. Edit Field Properties: Select the text field you wish to configure, right-click, and adjust the formatting and validation properties as needed.
- 4. Save Your Form: Click the 'Save' icon to preserve your interactive PDF form with all validation rules intact.

Frequently Asked Questions
Can I format the field as a number instead of using JavaScript?
Yes, in most robust PDF editors, you can open the field properties, select the Format tab, and choose 'Number' from the category dropdown. This restricts input without needing custom JavaScript, though custom scripts offer more tailored error messages.
Why isn't my JavaScript executing in the PDF?
Ensure that JavaScript is enabled in your PDF viewer's preferences. Some lightweight or browser-based PDF readers disable JavaScript for security reasons, which will prevent the custom validation script from running.
How do I allow decimal points in my numeric field using JavaScript?
To allow numbers and a decimal point, you need to modify your regular expression. Instead of testing for `/\D/` (which blocks everything except digits 0-9), use a pattern that permits digits and a single period, such as testing against `/[^0-9.]/`.




