How to Restrict a PDF Form Field to Numbers and Commas
Question details
The user needs to configure a PDF form field so it only accepts numbers and commas, specifically rejecting letters, spaces, descriptions, and other characters.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating or editing a PDF form that requires strict input validation, such as entering multiple department numbers separated by commas.
- Observed behavior
- By default, the PDF text field accepts any alphanumeric character. The goal is to restrict the input strictly to digits and commas to prevent invalid data entry.
Ensure you are using a comprehensive PDF editor like WPS Office or Adobe Acrobat that supports interactive form field properties and custom JavaScript formatting.
Apply a Custom Keystroke Script for Validation
Add a simple regular expression script in the field's formatting properties to instantly block invalid characters like letters and spaces during user input.
This method uses a Custom Keystroke Script triggered every time a user types into the field. By leveraging a standard regular expression, you can cleanly restrict the allowed input to digits and commas only.
Right-click the target text field in your PDF form and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Format' tab located at the top of the window.
Click the 'Select format category' dropdown menu and choose 'Custom'.
In the 'Custom Keystroke Script' text box, enter the following code: event.rc = /^[\d,]*$/.test(event.change);
Close the properties window, exit form editing mode, and test the field by typing letters or spaces. The field should now exclusively accept numbers and commas.

Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to create interactive forms, edit text, manage field properties, and apply advanced data validation scripts seamlessly.
- 1. Open your PDF in WPS Office: Launch WPS Office and open the PDF document containing your form.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form field tools.
- 3. Modify Field Properties: Right-click the target text field, select 'Properties', and navigate to the Format tab to set up custom validation rules.

Frequently Asked Questions
Can I restrict a PDF form field to only numbers without commas?
Yes, you can use the built-in 'Number' format category in the field properties under the Format tab. Alternatively, if you want to use a script to strictly allow only digits without separators, use the script: event.rc = /^\d*$/.test(event.change);
Why is my custom keystroke script not working in the PDF?
Ensure your PDF editor supports JavaScript execution and that JavaScript is not disabled in your application's security preferences. Also, double-check the script code for any missing brackets or syntax errors.
How do I allow spaces along with numbers and commas?
You can modify the regular expression in the custom keystroke script to include a space character inside the brackets. Update the code to: event.rc = /^[\d, ]*$/.test(event.change);




