Skip to main content
Participant
April 19, 2023
Answered

Combine 3 text fields into one, but omit a field if blank

  • April 19, 2023
  • 3 replies
  • 4845 views

Hi,

I'm trying to combine the text values from three separate text fields (LastName, FirstName, MiddleInitial) into another text field (FullName), but I want to omit the MiddleInitial field if it is blank.  I tried a few variations of the following javascript in the FullName field, but I can't get it to work:

 

{

var firstName = this.getField("FirstName").valueAsString;
var middleInitial = this.getField("MiddleInitial").valueAsString;
var lastName = this.getField("LastName").valueAsString;

 

if (middleInitial !="") {event.value = firstName + " " + lastName}
else {event.value = firstName + " " + middleInitial + " " + lastName};

}

 

Any help is much appreciated!

This topic has been closed for replies.
Correct answer JR Boulay

Remove the surrounding braces too:

 

var firstName = this.getField("FirstName").valueAsString;
var middleInitial = this.getField("MiddleInitial").valueAsString;
var lastName = this.getField("LastName").valueAsString;

if (middleInitial == "") {event.value = firstName + " " + lastName}
else {event.value = firstName + " " + middleInitial + " " + lastName};

3 replies

JR Boulay
Community Expert
Community Expert
April 21, 2023

We need to know in which field this script is placed and what action triggers it.

Acrobate du PDF, InDesigner et Photoshopographe
Participant
April 21, 2023

I just got it to work!  I should just say that I've been creating fillable PDF forms for years, but I am obviously a total rookie when it comes to javascript.  I'm trying to learn, so I really appreciate the help!

 

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
April 19, 2023

Remove the surrounding braces too:

 

var firstName = this.getField("FirstName").valueAsString;
var middleInitial = this.getField("MiddleInitial").valueAsString;
var lastName = this.getField("LastName").valueAsString;

if (middleInitial == "") {event.value = firstName + " " + lastName}
else {event.value = firstName + " " + middleInitial + " " + lastName};

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
September 7, 2023

I am doing something very similar but there are 44 total text fields and they are awards and their totals, separated by commas. Is there a way to scale the script you've written to do that?

Nesa Nurani
Community Expert
Community Expert
September 7, 2023

I am trying to combine 44 text fields into one but omitting any blank fields. I have attached my document to hopefully illustrate what I'm trying to do.


If you try to sum fields you can use built in calculation under 'Calculate' tab, select 'Value is the' then click on 'Pick' and select your fields?

Why you want to leave out fields that have 0 (blank), are you trying to calculate average?

Nesa Nurani
Community Expert
Community Expert
April 19, 2023

Use == not !=