Skip to main content
May 10, 2017
Answered

separate values in a string by commas if testbox is not empty

  • May 10, 2017
  • 2 replies
  • 804 views

I would like to combine values from several textboxes into a single textbox with the values separated by commas.  However, some of the textboxes may be empty so sometimes I get double commas next to each other or a value with a comma behind it but no additional value to follow.  here is the start of my code. 

// Get the field values, as strings

var s1 = getField("Text6").valueAsString;

var s2 = getField("Text12").valueAsString;

var s3 = getField("Text13").valueAsString;

var s4 =getField("Text14").valueAsString;

var s5 =getField("Text15").valueAsString;

//combine values with comma

event.value= s1 + "," + s2.......

Please help, thanks.

This topic has been closed for replies.
Correct answer try67

Try this code:

var fields =["Text6", "Text12", "Text13", "Text14", "Text15"];

var values = [];

for (var i in fields) {

    var f = this.getField(fields);

    if (f==null) {

        console.println("Error! The field doesn't exist: " + fields);

        continue;

    }

    if (f.valueAsString!="")

        values.push(f.valueAsString);

}

event.value = values.join(", ");

2 replies

Markarius
Inspiring
February 16, 2020

I have similar need...I need the textbox to pull values from 3 different textboxes... [<City>,  <State>  <Zip>] ... but if City or Zip code is empty (state is never empty as the default value for the textbox is "CA"), then it should not display the textbox... 

my old code was: 

//event.value = this.getField("Case City").valueAsString + "," + " " //+ this.getField("Case State").valueAsString + " " //+ this.getField("Case Zip Code").valueAsString;

 

The problem was that it would display something like this if i was missing city or zip:

   "    ,  CA "

 

Below is a modified version of the code by Ty67, to do what i stated above.. but for some reason it's not working... where did i go wrong?

var fields =["Case City", "Case Sate", "Case Zip Code"]; var values = []; for (var i in fields) { var f = this.getField(fields); if (f==null) { console.println("Error! The field doesn't exist: " + fields); continue; } if (f.valueAsString!="") values.push(f.valueAsString); } event.value = values.join(", ");

 

but for some reason the code isn't working... any idea where I went wrong?

Thanks

Thom Parker
Community Expert
Community Expert
February 17, 2020

It would be very helpful if your code was formatted corectly.

 

 This code is quite good, just one thing wrong. Here is the corrected code (correctly formated)

The issue is that "getField" needs to access a single element in the field name array. The original code does not include the element reference.

 

var fields =["Case City", "Case Sate", "Case Zip Code"]; 
var values = []; 
for (var i in fields) { 
    var f = this.getField(fields[i]); 
    if (f==null) { 
        console.println("Error! The field doesn't exist: " + fields); 
        continue; 
    } 
    if (f.valueAsString!="") values.push(f.valueAsString); 
} 

event.value = values.join(", ");

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 10, 2017

Try this code:

var fields =["Text6", "Text12", "Text13", "Text14", "Text15"];

var values = [];

for (var i in fields) {

    var f = this.getField(fields);

    if (f==null) {

        console.println("Error! The field doesn't exist: " + fields);

        continue;

    }

    if (f.valueAsString!="")

        values.push(f.valueAsString);

}

event.value = values.join(", ");

May 10, 2017

this worked, thanks!