Skip to main content
Participant
December 7, 2016
Answered

Extracting last and first initial into another field

  • December 7, 2016
  • 1 reply
  • 1122 views

Hello -

I'm having some difficulties with Javascript and was hoping someone could help.

I have a full name field (Field Name: Project_Initiator) and would like to extract the last name and first initial into another field  (i.e. Dan Smith = Smith, D.).

I am using Adobe Acrobat XI Pro Mac.

Thank you for your help.

This topic has been closed for replies.
Correct answer try67

OK, then you can use this code as the custom calculation script of the Initial field:

var fullName = this.getField("Project_Initiator").valueAsString;

if (fullName=="") event.value = "";

else {

    var names = fullName.split(" ");

    event.value = names[names.length-1] + ", " + names[0].substring(0,1)+".";  

}

1 reply

try67
Community Expert
Community Expert
December 7, 2016

What if someone fills in three names? How will you know if it's two first names, or two last names?

I would recommend you don't do this automatically. It's too risky and if you get it wrong people can get really pissed off.

DSM0802Author
Participant
December 7, 2016

It's internal - and for my use only.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 7, 2016

OK, then you can use this code as the custom calculation script of the Initial field:

var fullName = this.getField("Project_Initiator").valueAsString;

if (fullName=="") event.value = "";

else {

    var names = fullName.split(" ");

    event.value = names[names.length-1] + ", " + names[0].substring(0,1)+".";  

}