PDF Editing Issues

How to Require One of Two PDF Form Fields using JavaScript

Maira MehtabMaira Mehtab Nov 3, 2026 869 views

Question details

The user needs to configure a PDF form so that filling out at least one of two specific text fields is mandatory.

How to Require One of Two PDF Form Fields
Product
PDF Editor
Device & OS
not provided
Scenario
Creating or editing an interactive PDF form where users must provide at least one piece of information (such as a phone number or an email address) but can optionally provide both.
Observed behavior
Requires applying a custom JavaScript validation script to dynamically adjust the 'required' property of the fields based on user input.
Before you start

Ensure you have a PDF editing tool that supports custom JavaScript for form fields, and note the exact names of the two text fields you plan to configure.

Solution 1Recommended

Apply Custom JavaScript Validation to Form Fields

Use a simple JavaScript snippet on both fields to dynamically toggle their required status based on whether the other field is empty.

By setting both fields to 'required' initially and attaching a custom validation script, the PDF reader will dynamically assess the fields. When a user types into one field, the script automatically removes the mandatory requirement from the other field, ensuring at least one is filled before submission.

1
Open form editing mode

Open your PDF document in your form editor and ensure both target text fields have already been created and named.

2
Set initial required status

Right-click on the first field, select 'Properties', and check the box to make the field 'Required'. Do the exact same thing for the second field.

3
Add validation script to the first field

In the Properties of the first field, navigate to the 'Validate' tab. Select 'Run custom validation script' and enter the following code, replacing 'Field2_Name' with the exact name of the second field: this.getField("Field2_Name").required = (event.value == ""); event.target.required = true;

4
Add validation script to the second field

Open the Properties of the second field, go to the 'Validate' tab, and enter the mirrored script, replacing 'Field1_Name' with the exact name of the first field: this.getField("Field1_Name").required = (event.value == ""); event.target.required = true;

Apply Custom JavaScript Validation to Form Fields
Testing the Form: After applying the scripts, save the document and exit form editing mode. Test the functionality by attempting to submit the form with both fields blank, with only one filled, and with both filled to ensure the validation works correctly.

Edit and Fill PDF Forms Effortlessly with WPS Office

WPS Office provides a comprehensive PDF editor that allows you to easily create, edit, fill, and sign interactive PDF forms without needing expensive or heavy software.

  1. 1. Open your PDF: Launch WPS Office and open your interactive PDF document from your local storage.
  2. 2. Access the Form tools: Navigate to the 'Form' or 'Edit' tab in the top ribbon to access form filling and editing tools.
  3. 3. Fill out or Modify Fields: Click directly into the text fields to enter your data, or use the form editing options to adjust field properties as needed.
Easily fill out interactive PDF forms with advanced validationsHighly compatible with Adobe Acrobat PDF formats and form fieldsLightweight, fast, and completely free for basic viewing and editingSeamlessly merge, split, or convert your PDF documents
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my JavaScript validation working on my PDF form?

Ensure that you have entered the exact, case-sensitive field names inside the this.getField("...") function. Also, verify that your specific PDF viewer supports JavaScript execution, as many browser-based viewers disable it for security reasons.

Can I require one of three or more fields instead of just two?

Yes, but the JavaScript becomes more complex. You will need to write a script that evaluates the values of all other fields in the group and dynamically adjusts the required status based on whether all of them are currently empty.

Will this custom JavaScript work on mobile PDF readers?

Support for custom JavaScript in PDF forms varies heavily on mobile devices. Many default mobile PDF readers do not execute JavaScript at all, which means users may bypass the validation unless they open the document in a fully featured PDF application.

How do I find the exact name of a PDF form field?

While in form editing mode, double-click the form field or right-click it and select 'Properties'. The exact field name will be displayed in the 'General' tab.