Skip to main content
Participant
April 23, 2019
Answered

Truncate a field (Name) to create Initials

  • April 23, 2019
  • 2 replies
  • 1249 views

Hi - I am brand new at this... Please be gentle.

I have an Acrobat form with a persons name in two fields. (First name and Last name) I would like to automatically create a third read only field with just the first letters of each name.

Any ideas? Thanx

This topic has been closed for replies.
Correct answer Thom Parker

Yes, a script like this will work. Put it in the calculation event.

var cFName = this.getField("First Name").value;

var cLName = this.getField("Last Name").value;

event.value = ((cFName.length)?cFName[0]:"") + ((cLName.length)?cLName[0]:"");

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 23, 2019

Yes, a script like this will work. Put it in the calculation event.

var cFName = this.getField("First Name").value;

var cLName = this.getField("Last Name").value;

event.value = ((cFName.length)?cFName[0]:"") + ((cLName.length)?cLName[0]:"");

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
April 23, 2019

That will work, but only if each name is only one word. If the first name is "John Paul" and the last name is "George", for example, it will only return "JG", not "JPG".

try67
Community Expert
Community Expert
April 23, 2019

You can use something like this as the custom calculation script of the initials field:

var fullName = this.getField("First name").valueAsString + " " + this.getField("Last name").valueAsString;

var words = fullName.split(" ");

var initials = "";

for (var i in words) initials+=words.charAt(0).toUpperCase();

event.value = initials;