How to Dynamically Change Text Color for Fractions in Acrobat PDF Forms
Question details
The user wants to automatically format a calculated field in an Adobe Acrobat form so that its text color changes from black to red whenever the calculated result is a fraction.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Designing a fillable PDF form with calculated fields that require visual alerts for non-integer (fractional or decimal) values.
- Observed behavior
- The user needs to implement conditional formatting to make the text color switch dynamically between red (for fractions) and black (for whole numbers).
Ensure you are using Adobe Acrobat Pro or a professional PDF editor that supports advanced form editing, as standard PDF readers do not allow you to add or modify JavaScript validation scripts.
Apply a Custom JavaScript Validation to the Form Field
Use a simple JavaScript snippet in the field's validation properties to detect fractions and change the text color dynamically.
This method utilizes the modulo operator in JavaScript to check if the calculated value is a whole number. If there is a remainder (indicating the number is a fraction or decimal), the script forces the text color of that specific field to turn red. Otherwise, it remains black.
Open your PDF in Adobe Acrobat, navigating to 'Tools' in the top menu and clicking on 'Prepare Form' to enable form editing.
Right-click on the specific calculated field that you want to format and select 'Properties' from the drop-down menu.
In the Field Properties dialog box that appears, click on the 'Validation' tab at the top.
Select the 'Run custom validation script' radio button and click the 'Edit' button. Paste the following code into the editor: 'if(event.value){ var n = Number(event.value) % 1 === 0; if(!n) event.target.textColor = color.red; else event.target.textColor = color.black; }'.
Click 'OK' to save your script, then click 'Close' on the Properties window. Test your form by entering values that result in both whole numbers and fractions to see the color change in action.

Manage and Fill PDF Forms Seamlessly with WPS PDF
WPS Office includes a powerful built-in PDF editor that allows you to easily view, fill out, and manage interactive PDF forms without needing heavy or expensive dedicated PDF software.
- 1. Install WPS Office: Download and install WPS Office from the official website to access the integrated WPS PDF tool.
- 2. Open Your PDF Form: Launch WPS Office and open your interactive PDF document directly within the application's interface.
- 3. Fill and Edit: Use the 'Form' or 'Edit' tab to interact with form fields, input data, and manage your document effortlessly.
- 4. Save and Export: Save your completed PDF form locally, or easily convert it to other Office formats like Word or Excel if necessary.

Frequently Asked Questions
Can I use this JavaScript validation in free PDF readers?
No, you must use Adobe Acrobat Pro or a professional PDF editor to add or edit these JavaScript validations. Standard free readers can execute the scripts when users fill the form, but they cannot be used to create or apply them.
Why is my calculated field not changing color immediately?
Ensure that the calculation order is set correctly in your form. The field must calculate its value before the validation script runs. You can check this under 'Tools' > 'Prepare Form' > 'More' > 'Set Field Calculation Order'.
Can I change the text color to something other than red?
Yes. In the provided JavaScript code, you can replace 'color.red' with another predefined color such as 'color.blue', 'color.green', or you can define a custom RGB color using '["RGB", r, g, b]'.
Does this script work if the user types the fraction manually?
Yes, by placing the script in the field's Validation tab, it ensures the color change triggers whether the value is auto-calculated by the form or manually inputted by the user.




