Skip to main content
Participating Frequently
September 7, 2023
Answered

If a text field is zero, display nothing and check the next text field.

  • September 7, 2023
  • 2 replies
  • 2225 views

I am attempting to write a script that checks the value of a field and displays fixed text + the value. If the value is zero, it should display nothing and check the next field.

 

If there are multiple fields with a value greater than zero, it should display them with a comma separating them.

 

The purpose of this document is to allow someone to choose how many awards they have and then display them in a specific format.

For example:

If someone indicates that they have an achievement award and two commendations, it should display as "UTACH-1, UTCOM-2". If they have zero achievement awards but two commendations, it'll display as "UTCOM-2".

 

The issue I'm running into is that there are 44 total awards so I have to come up with an eloquent solution rather than just do a bunch of "if else" commands.

This topic has been closed for replies.
Correct answer Thom Parker

That is a great question, sir! I am still trying to wrap my head around JavaScript. I have attached the document I am trying to create in order to help illustrate what I'm working with.

 

Thank you for the help with the array. I've been combing through the Adobe JavaScript Reference to try and get something working here.


After looking at this more closely, You're best strategy is to place the display text in the tooltip text. 

 

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");

 

 

Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior. 

You can add new fields and change the display text without changing the script. 

 

 

2 replies

Nesa Nurani
Community Expert
Community Expert
September 7, 2023

Use this:

var medal = ["MOVSM-", "CGACH-", "AFAM-", "NAM-", "NAMV-", "AAM-", "JSAM-","CGCM-", "AFCM-", "NCM-", "NCCDV-", "ARCOM-V-", "ARCOM-", "JSCM-","JSCMV-", "AFAAM-", "AM-V-", "AM-", "MSM-", "DMSM-", "BSM-V-","BSM-", "ASM-", "CGM-", "NMCM-", "SM-", "DFC-", "LM-", "DSSM-","SS-", "CGDSM-", "DSM-", "DDSM-", "AFC-", "NC-", "DSC-", "MH-","UTACH-", "UTCOM-", "UTJCM-", "UTMOM-", "UTJMM-", "UTCRS-", "UTMOV-"];
var score = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var str = [];

for (var i = 1; i <= 44; i++) {
 var newScore = Number(this.getField("Text"+i).valueAsString);
  if(newScore !== 0){
   score[i-1] = newScore;}}

for(var j=0; j<score.length; j++){
 if(score[j] !== 0)
  str.push(medal[j]+score[j]);}

event.value = str.join(",");
Participating Frequently
September 7, 2023

Thank you! This worked perfectly!

Participating Frequently
September 7, 2023

I do have this script that does work but I'd very much like to avoid the headache of writing out all of the "if else" commands I was talking about.

 

var a = this.getField("Text38").value;
var b = this.getField("Text39").value;

if (a == "0")

{

event.value = ""

} else {

event.value = "UTACH-" + a + ", " + "UTCOM-" + b

}

Thom Parker
Community Expert
Community Expert
September 7, 2023

Name all of the award fields "Award.Field1", "Award.Field2", etc. 

Then this calculation script will collect all the data

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aWards.push("UTACH-" + oFld.value);});
event.value = aAwards.join(",");

 

This is nearly as elegant as you can get.

You did not provide a way to differentiation between the "UTACH" and "UTCOM". How were you planning on doing this? 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
September 7, 2023

That is a great question, sir! I am still trying to wrap my head around JavaScript. I have attached the document I am trying to create in order to help illustrate what I'm working with.

 

Thank you for the help with the array. I've been combing through the Adobe JavaScript Reference to try and get something working here.


After looking at this more closely, You're best strategy is to place the display text in the tooltip text. 

 

var aAwards = [];
this.getField("Award").getArray().forEach(function(oFld){if(oFld.value != 0) aAwards .push(oFld.userName + "-" + oFld.value);});
event.value = aAwards.join(",");

 

 

Note that the script is generic and provides excellent separation between the number of fields, text, and the behavior. 

You can add new fields and change the display text without changing the script. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often