How to Display Custom Messages Based on PDF Form Values using JavaScript
Question details
The user needs to display a specific text code or message in a PDF form when a numeric field contains a matching value.

- Product
- Device & OS
- not provided
- Scenario
- Creating or editing interactive PDF forms where numeric inputs need to be automatically translated into predefined text codes.
- Observed behavior
- Looking for the appropriate Acrobat JavaScript approach to either replace the entered numeric value with a text code or populate a different field with the corresponding message.
Ensure your PDF editing software supports JavaScript for interactive form fields. Familiarize yourself with accessing form field properties and navigating to the Validate or Calculate tabs.
Use a Custom Validation Script to Replace the Entered Value
Apply a custom validation script if you want the numeric value entered by the user to be immediately replaced by a specific text code within the same field.
This method uses a JavaScript validation script directly on the input field. When the user enters a specific number and exits the field, the script evaluates the input and automatically replaces it with the corresponding text string.
Open your interactive PDF in your PDF editor (such as Adobe Acrobat) and enter Form Editing mode.
Right-click the numeric input text field and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Validate' tab to set up custom data validation rules.
Select 'Run custom validation script' and click 'Edit'. Enter the following code: if (event.value == 0.8) { event.value = 'Code 1A'; } else if (event.value == 0.95) { event.value = 'Code 1B'; } Click OK to save.

Use a Custom Calculation Script for a Separate Field
Use this approach if you want to keep the original numeric input visible in one field while displaying the corresponding message or code in a separate field.
Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to easily view, fill, create, and manage interactive PDF forms without needing heavy or expensive software.
- 1. Download and install WPS Office: Get the free version of WPS Office from the official website and install it on your device.
- 2. Open your PDF form: Launch WPS Office and open your interactive PDF file directly within the application.
- 3. Fill and manage the form: Use the built-in PDF tools to interact with form fields, input data, and leverage smart document features.
- 4. Save and export: Save your filled PDF form securely or share it directly from the WPS interface.

Frequently Asked Questions
Will these JavaScript validation rules work in all PDF viewers?
Not necessarily. While they will work perfectly in advanced PDF editors like Adobe Acrobat, basic browser-based PDF viewers or simple mobile apps might not support executing embedded JavaScript in interactive forms.
Can I use strings instead of numbers for the validation trigger?
Yes. You can modify the JavaScript to evaluate text strings by placing the target value in quotation marks, such as: if (event.value == "Apple") { event.value = "Code A"; }
Why is my custom calculation script not updating immediately?
Calculation scripts typically trigger when a field loses focus. Ensure that you have pressed 'Enter' or clicked outside the numeric input field for the JavaScript to execute and update the target field.
How do I clear the custom message if the numeric field is left blank?
You can add a condition at the beginning of your script to check for a blank value: if (event.value == "") { event.value = ""; } to ensure the field resets properly when data is deleted.




