How to Enable a PDF Comments Field After Required Fields Are Completed
Question details
The user needs to dynamically lock a Comments field in a PDF form using JavaScript, ensuring it unlocks only when the prerequisite Item and Quantity fields are filled out.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with conditional logic where specific text fields must remain read-only until other required fields are completed by the user.
- Observed behavior
- The Comments field remains locked (read-only) and clears its value automatically unless both the Item and Quantity fields contain valid data.
Ensure you are using a PDF editor that supports interactive form fields and custom JavaScript calculations, and verify the exact names of your prerequisite fields.
Add a Custom Calculation Script to the Comments Field
Use JavaScript within the field properties to conditionally lock the Comments field and clear stale entries when prerequisite fields are empty.
By applying a custom calculation script directly to the target field, the PDF reader will evaluate the condition every time data changes in the form. If the required fields are empty, the script locks the target field and wipes any previously entered text.
Open your interactive PDF in 'Prepare Form' or 'Form Editing' mode depending on your PDF software.
Right-click the 'Comments' field (the field you want to lock) and select 'Properties' from the context menu.
In the Properties window, click on the 'Calculate' tab and choose the 'Custom calculation script' option.
Click the 'Edit' button and paste the following code: if (this.getField('Item').valueAsString == '' || this.getField('Quantity').valueAsString == '') { event.target.readonly = true; event.value = ''; } else { event.target.readonly = false; }
Click 'OK' to apply the script, close the properties window, and exit form editing mode to test if the Comments field behaves as expected.

Create and Manage Interactive PDF Forms Easily with WPS Office
WPS Office provides robust and intuitive PDF editing features. You can quickly insert text fields, checkboxes, and manage form properties without complex coding, making it the perfect all-in-one suite for your document workflows.
- 1. Open Your PDF: Launch WPS Office, open your PDF document, and navigate to the 'PDF Edit' or 'Form' tab.
- 2. Insert Form Controls: Select 'Text Field' from the toolbar and click on the document to place the interactive field.
- 3. Adjust Properties: Double-click the inserted field to open the Properties dialog, where you can customize its appearance, name, and default state.
- 4. Save the Form: Once your fields are set, save the PDF to share a fillable, interactive document with your team.

Frequently Asked Questions
Can I make a PDF form field required before submission?
Yes, you can enforce this through the form field properties. Open the field's Properties window, go to the 'General' tab, and check the 'Required' box. This ensures the user is prompted to fill out that specific field before the form can be submitted or printed.
Why is my JavaScript calculation not working in the PDF form?
Ensure that your field names in the script exactly match the names defined in the form properties. Field names are case-sensitive (e.g., 'Item' is different from 'item'). Additionally, verify that the script is placed in the 'Calculate' tab of the target field rather than the source fields.
How do I hide a PDF form field until another element is clicked?
You can achieve this by adding a 'Run a JavaScript' action to a button or checkbox's 'Mouse Up' trigger. Use the display property in your script, such as 'this.getField("TargetField").display = display.visible;', to conditionally show the hidden field.




