Copy link to clipboard
Copied
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.
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)+".";
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
It's internal - and for my use only.
Copy link to clipboard
Copied
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)+".";
}
Copy link to clipboard
Copied
PERFECT! Thank you so very very much!!
Copy link to clipboard
Copied
Hi try67, I'm in that very situation. I have come across someone with three names. I need to extract just the first initial of each name.
How would I go about including the middle initial on those rare occasions when it comes up?
Thanks!
Copy link to clipboard
Copied
Try this code:
var fullName = this.getField("Full Name").valueAsString;
if (fullName=="") event.value = "";
else {
var names = fullName.split(" ");
var initials = "";
for (var i=0; i<names.length; i++) initials+=names.substring(0,1).toUpperCase()+".";
event.value = initials;
}
Copy link to clipboard
Copied
Thanks! Just what I needed. Much appreciated.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more