Skip to main content
Participant
February 22, 2022
Question

Trying to create a JavaScript code for conditional logic on a text box formatted as NUMBERS

  • February 22, 2022
  • 2 replies
  • 535 views

Hi,

 

I'm trying to create a script that will change the fill color of a text box depending on the value.  The text box is formatted to numbers only, with no decimals.  The text has transparent fill and border by default and will be empty by default. 

 

The desired behavior is if text box value = 0, 1, 2, or 3 then change fill color to red and border color to blue.  If blank or > 3 then fill and border color should be transparent.  The problem I'm having is that if the text box is blank, it applies the red fill color/blue border color.  I have the trigger as On Focus and again On Blur, trying to account for what our staff may potentially do when working with this document.  My company has ADOBE ACROBAT STANDARD DC.

 

Any guidance would be appreciated!

 

My code so far:

var nRemSup = this.getField("Rm Supply text box 1").value;
var fld = this.getField("Rm Supply text box 1");

if((nRemSup < 4) && (nRemSup >= 0)) {
fld.fillColor = ["RGB", 1,0,0];
fld.strokeColor = ["RGB",0,0,1];}

else if(nRemSup >= 4) {
fld.strokeColor = color.transparent;
fld.fillColor = color.transparent;}

else if(nRemSup = "") {
fld.strokeColor = color.transparent;
fld.fillColor = color.transparent;}

 

Thank you, stay safe

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
February 22, 2022

You've made a classic error.

Change this line:

else if(nRemSup = "") {

To:

else if(nRemSup == "") {

Bernd Alheit
Community Expert
Community Expert
February 22, 2022

The script will not come to this point.

Mini2425Author
Participant
March 15, 2022

Apologies for the late response!  Appreciate everyone's time and efforts here.  Bernd seems to be spot on, tried the adjustment suggested by Try67 but I get the same result.  Blank seems to be the same as 0 with number formatting.

 

Thank you, stay safe!

Bernd Alheit
Community Expert
Community Expert
February 22, 2022

When the text field is empty you will get 0 as value.