Skip to main content
August 18, 2020
Answered

Script to determine value of text box

  • August 18, 2020
  • 2 replies
  • 2722 views

Good day,

I am attempting to create an interactive PDF form, but I have absolutely no coding background whatsoever. That being said, what I would like to do is fairly simple.

 

I have set up a form with several of radio buttons with values associated. In each row, I have a text box (Text01, Text02, etc.) that displays the value selected by the corresponding radio button. At the bottom of the page, there is a text box that totals all of the text boxes and displays that number (TextTotal).

 

What I would like is for a separate text box (TextResult) to display a string of conditionally formatted text based on the value of TextTotal. For example

 

if TextTotal <= 5, then TextResult = "Green";

if TextTotal  >5 but <= 10, then TextResult = "Yellow";

if TextTotal <10, then TextResult = "Red"

 

How, and in what field would I program this??

 

Thanks so much!

This topic has been closed for replies.
Correct answer try67

As the custom calculation script of TextResult enter the following code:

 

var v = Number(this.getField("TextTotal").valueAsString);
if (v<=5) {
	event.value = "Green";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "Yellow";
	event.target.textColor = color.yellow;
} else {
	event.value = "Red";
	event.target.textColor = color.red;
}

 

2 replies

August 19, 2020

Fantastic! Thanks so much. I do have one more question. 

 

I am making a risk/authorization matrix. Each radio button has a value of 1, 2, or 3 which corresponds to the the riskiness of the activity (3 being the most). The form accumulates these points and determines who the authority to approve based on the score (TextTotal). However, if more than 2 instances of the value 3 occurs, the total score is ignored and that will govern the authorization level outcome.

 

How can I incorporate that?

 

Thanks so much! This community is awesome.

try67
Community Expert
Community Expert
August 19, 2020

You need to create two counter variables in your code: One for the general total and one for the total of "3" options selected. At the end, if the value of the first one is above the threshold OR the value of the second one is above 2 then show the desired level outcome, otherwise proceed as usual. If you post your current code I can help you adjust it.

August 19, 2020

Thank you so much. This is really helping me out!

 

var v = Number(this.getField("TotalScore").valueAsString);
if (v<=5) {
event.value = "FAO";
event.target.textColor = color.green;
} else if (v<=10) {
event.value = "COS Ops / SOA OC";
event.target.textColor = color.yellow;

} else if (v>10) {
event.value = "CO";
event.target.textColor = color.red;
} else {
event.value = "FAO";
event.target.textColor = color.green;
}

 

If the indents don't show up, please let me know what how to do that in future because I think I'll be leveraging the help of these forums more and more knowing how responsive they are. I realse what is above probably has some redundancy in it (the FAO return string), but I couldn't make it behave the way I wanted any other way.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 18, 2020

As the custom calculation script of TextResult enter the following code:

 

var v = Number(this.getField("TextTotal").valueAsString);
if (v<=5) {
	event.value = "Green";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "Yellow";
	event.target.textColor = color.yellow;
} else {
	event.value = "Red";
	event.target.textColor = color.red;
}

 

Inspiring
August 19, 2020

And what if "TextTotal" is empty? 

try67
Community Expert
Community Expert
August 19, 2020

In the code above it will be treated the same as zero.