How to Validate a PDF Text Field Only When It Contains Text
Question details
The user needs to configure an Acrobat PDF form text field so that validation only occurs when text is actively entered, while restoring the field's default value if left blank.

- Product
- Adobe Acrobat PDF
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where a specific text field requires a certain keyword to be valid, but should ignore validation and revert to a default state if the user leaves it empty.
- Observed behavior
- By default, form validation scripts trigger on any change, potentially causing errors on empty fields. The goal is to conditionally check the input value before applying the validation rule.
Ensure you have access to a PDF editor with form preparation and custom JavaScript capabilities, and verify that your target text field already has a 'Default Value' set in its properties.
Add a Custom JavaScript Validation Script
Use a custom JavaScript function in the field's validation settings to check if the field is blank, restore the default value if necessary, and only validate the presence of required text when the field is populated.
Acrobat JavaScript allows you to intercept user input before it is finalized. By utilizing the event.value and event.target.defaultValue properties, you can create a conditional validation rule that bypasses empty submissions and strictly enforces formatting on actual text entries.
Open your PDF document in your advanced PDF editor (such as Adobe Acrobat Pro) and access the 'Prepare Form' or 'Edit Form' tool.
Right-click the specific text field you want to validate and select 'Properties' from the context menu.
In the Text Field Properties dialog box, click on the 'Validate' tab. Select the radio button for 'Run custom validation script'.
Click the 'Edit' button and paste the following code: `if (!event.value) { event.value = event.target.defaultValue; return; } if (event.value.toUpperCase().indexOf("YOUR_WORD") === -1) { app.alert("Please try again."); event.rc = false; }`. Replace "YOUR_WORD" with your required text.
Click 'OK' to close the JavaScript editor, then click 'Close' on the properties window. Preview the form to test entering a blank value versus incorrect text.

Manage and Fill PDF Forms Easily with WPS Office
While complex JavaScript validation requires specialized form creation tools, filling out, editing, and managing interactive PDF forms is seamless with WPS Office. It provides a lightweight, highly compatible PDF solution for all your everyday document needs.
- 1. Download and install: Get the free WPS Office suite from the official website and install it on your device.
- 2. Open your PDF file: Launch WPS PDF and open the interactive PDF form you need to fill or edit.
- 3. Fill the form fields: Click on the text fields directly to type your information. Validation scripts embedded in standard PDFs will operate seamlessly as you type.
- 4. Save and export: Once completed, click 'Save' or 'Export to PDF' to preserve your inputs and share the finalized document.

Frequently Asked Questions
Why does my PDF validation script trigger an error when the field is left empty?
Validation scripts trigger automatically whenever the field loses focus or its value changes. If you do not explicitly tell the script to exit when the value is blank (using a condition like `if (!event.value)`), it will attempt to validate the empty string, which usually fails your conditions.
How do I set the default value for a PDF text field?
In form editing mode, right-click the text field and open 'Properties'. Go to the 'Options' tab and type your desired default text into the 'Default Value' box. This is the value that `event.target.defaultValue` will reference.
Can I use regular expressions for advanced PDF text validation?
Yes. Within the custom validation script window, you can utilize standard JavaScript Regular Expressions (RegExp) to test `event.value` against complex patterns, such as specific alphanumeric formats, dates, or email structures.




