Skip to main content
Participant
January 16, 2017
Answered

Can the color of text be changed based on the what the text field ends with?

  • January 16, 2017
  • 1 reply
  • 7113 views

I am trying to change the color of the text based on what the text field ends with.

For example:

"Rewards Price $9.96" - Orange

"Sale Price 9.97" - Red

If the text box ends in .96 it automatically turns the text orange, but if it ends in anything else it ends in red.

Is this possible?

This topic has been closed for replies.
Correct answer try67

Sure. You can use something like this as the field's custom validation script to achieve it:

if (/\.96$/.test(event.value)) event.target.textColor = ["RGB", 1, 0.66, 0];

else event.target.textColor = ["RGB", 1, 0, 0];

Edit: fixed code.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 16, 2017

Sure. You can use something like this as the field's custom validation script to achieve it:

if (/\.96$/.test(event.value)) event.target.textColor = ["RGB", 1, 0.66, 0];

else event.target.textColor = ["RGB", 1, 0, 0];

Edit: fixed code.

Participant
January 17, 2017

I pasted the code, adjusted to color to CMYK and added the colors needed, but the text turned black and doesn't change at all...

if (/\.96/.test(event.value)) event.target.textColor = ["CMYK", 12, 60, 98, 1]; 

else event.target.textColor = ["CMYK", 18, 92, 100, 8];

try67
Community Expert
Community Expert
January 17, 2017

You should not change code unless you know what you're doing... First of all, put back the dollar sign where it was. It's not used as such. It's an indicator that the string appears at the end of the text.

The values of a Color object in Acrobat JS are from 0 to 1, not from 0 to 255, as you might be used to.

So divide your current values by 255 to get the correct results.