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
Participant
April 19, 2023

I copied the script and pasted it in my FullName field, but it still isn't working. 😞

try67
Community Expert
Community Expert
April 19, 2023

You need to be more specific than that. Does it produce an output at all, but is it wrong? If so, in what way is it wrong? If there's no output at all, are there any error messages in the JS Console when you use it?

Without these basic details we can't help you.

Nesa Nurani
Community Expert
Community Expert
April 19, 2023

Use == not !=