Skip to main content
Participating Frequently
February 23, 2016
Answered

Automatic field color change for different value range?

  • February 23, 2016
  • 16 replies
  • 2500 views

Hi, I am a new user to Acrobat Pro and I am trying to figure out if it is possible to have the field color change when calculated value is within a specific range.  I have a calculated field box and I want it to turn yellow when it reaches a specific numerical range, and red when it gets to another.  A font color change would work too..

This topic has been closed for replies.
Correct answer George_Johnson

So, here is what I have...It accepts it, but nothing happens.

(function(){

var v = +event.value;

if (v<16){

event.target.fillColor=color.green;

}

if (16<v>27){

event.target.fillColor=color.yellow;

}

//Value is greater than 26

event.target.fillColor=color.red;

})();


The correct code could be:

(function() {

    var v = +event.value;

    if (v < 16) {

        event.target.fillColor = color.green;

        return;

    }

    if (v < 27) {

        event.target.fillColor = color.yellow;

        return;

    }

    // Value is greater than 27

    event.target.fillColor = color.red;

})();

This line of code in particular isn't valid:

if (16<v>27){

Something like this would be:

if (v >= 16 && v < 27) {

which translated to English is: If the value of the variable v is greater than or equal to 16 and less than 27...

So another functionally equivalent script could be:

var v = +event.value;

if (v < 16) {

    event.target.fillColor = color.green;

} else if (v >= 16 && v < 27) {

    event.target.fillColor = color.yellow;

} else {  // Value is greater than or equal to 27

    event.target.fillColor = color.red;

}

I'm not sure if it's exactly what you want, but you should be able to revise it to suit.

16 replies

Inspiring
February 23, 2016

Sure it's possible. Are you currently using a custom calculation script or one of the built-in calculation options?

Participating Frequently
February 23, 2016

‌Just the built in sum of selected fields.

Inspiring
February 23, 2016

You can use a custom Validation script for the field, something like the following:

// Custom Validate script for text field

(function () {

    // Set background color to white if blank

    if (!event.value) {

        event.target.fillColor = color.white;

        return;

    }

    // Convert value to a number

    var v = +event.value;

    if (v < 5) {

        event.target.fillColor = color.red;

        return;

  }

  if (v < 10) {

        event.target.fillColor = color.yellow;

        return;

  }

  // Value is greater than 10

    event.target.fillColor = color.green;

})();