Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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};
Copy link to clipboard
Copied
Use == not !=
Copy link to clipboard
Copied
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};
Copy link to clipboard
Copied
I copied the script and pasted it in my FullName field, but it still isn't working. 😞
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
There is no output at all. I get the following error: [ Line: 00004 { GeneralError } ] : 'event.value' Unspecified error cause.
Copy link to clipboard
Copied
I should have specified that Line 4 is the IF statement.
Copy link to clipboard
Copied
This suggests a more fundamental issue in the application. Try running a Repair Installation from the Help menu.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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;}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Can you describe exactly what you try to do?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Check your post for solution
Copy link to clipboard
Copied
We need to know in which field this script is placed and what action triggers it.
Copy link to clipboard
Copied
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!