Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

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

Community Beginner ,
Apr 19, 2023 Apr 19, 2023

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!

TOPICS
Rich media and 3D
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Apr 19, 2023 Apr 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 Photoshoptographe

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Use == not !=

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 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 Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2023 Apr 19, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2023 Apr 20, 2023

There is no output at all.  I get the following error: [ Line: 00004 { GeneralError } ] : 'event.value' Unspecified error cause.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 20, 2023 Apr 20, 2023

I should have specified that Line 4 is the IF statement.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2023 Apr 21, 2023

This suggests a more fundamental issue in the application. Try running a Repair Installation from the Help menu.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2023 May 17, 2023

I am trying something similar 

 

var PFirst = this.getField("PFirstName").valueAsString;
var PMi = this.getField("PMidInit").valueAsString;
var PLast = this.getField("PLastName").valueAsString;

if (PMi == "") {event.value = PFirst + " " + PLast}
else {event.value = PFirst + " " + PMi + " " + PLast};

 

 

and getting the following error:

 

SyntaxError: missing ; before statement
6:AcroForm:Plaintiff:Calculate

 

I am sure I am overlooking something simple, but I just can't "see" what is missing

 

Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2023 May 17, 2023

I'm not getting any errors, although you are missing semicolon in 'if' statement.

Try this:

var PFirst = this.getField("PFirstName").valueAsString;
var PMi = this.getField("PMidInit").valueAsString;
var PLast = this.getField("PLastName").valueAsString;

if (PMi == "")
{event.value = PFirst + " " + PLast;}
else
{event.value = PFirst + " " + PMi + " " + PLast;}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023

Can you describe exactly what you try to do?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 2023

The goal is to have the text box display a fixed text + the value of any given award, separated by commas in the event there are multiple. For example: Someone indicated that they have 1 Navy Achievement Medal and 2 Army Commendation. It should display as "NAM-1, ARCOM-2". All other fields that are left at zero should not display in the "total" text box.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2023 Apr 21, 2023

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


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 21, 2023 Apr 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!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines