PDF Editing Issues

How to Reject Non-Numeric Input in PDF Forms Using JavaScript

John WilsonJohn Wilson Nov 3, 2026 869 views

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.

How to Reject Non-Numeric Input in PDF Forms Using JavaScript
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.
Before you start

Ensure you are using a PDF editor that supports JavaScript execution and allows you to modify advanced form field properties.

Solution 1Recommended

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.

1
Access Field Properties

Open your PDF form in edit mode. Right-click the text field you want to modify and select 'Properties' from the context menu.

2
Locate the Keystroke Event

Navigate to the 'Format' or 'Actions' tab (depending on your specific PDF editor) and find the 'Custom Keystroke Script' or 'Run a JavaScript' trigger.

3
Insert the JavaScript Code

Enter the following code into the script editor window: if (/\D/.test(event.value)) { app.alert("Only numbers are allowed!"); event.rc = false; }

4
Save and Test

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.

Understanding the Code: The `/\D/` regular expression checks the input for any non-digit character. If a non-digit is found, `app.alert` displays the warning pop-up, and `event.rc = false` blocks the character from being registered in the field.
Advanced PDF Editing

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. 1. Open Your PDF: Launch WPS Office and open your PDF document containing the form.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all interactive form creation tools.
  3. 3. Edit Field Properties: Select the text field you wish to configure, right-click, and adjust the formatting and validation properties as needed.
  4. 4. Save Your Form: Click the 'Save' icon to preserve your interactive PDF form with all validation rules intact.
Supports advanced PDF form field properties and validation rulesFully compatible with standard PDF formats across the industryLightweight application that handles PDFs, Word, Excel, and PowerPointCross-platform support for Windows, Mac, and mobile devices
microsoft office alternative - wps office

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.]/`.