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

Extracting last and first initial into another field

New Here ,
Dec 07, 2016 Dec 07, 2016

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.

TOPICS
Acrobat SDK and JavaScript
1.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

correct answers 1 Correct answer

Community Expert , Dec 07, 2016 Dec 07, 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)+".";  

}

Translate
Community Expert ,
Dec 07, 2016 Dec 07, 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.

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
New Here ,
Dec 07, 2016 Dec 07, 2016

It's internal - and for my use only.

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 ,
Dec 07, 2016 Dec 07, 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)+".";  

}

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
New Here ,
Dec 07, 2016 Dec 07, 2016

PERFECT! Thank you so very very much!!

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 ,
Dec 18, 2017 Dec 18, 2017

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!

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 ,
Dec 19, 2017 Dec 19, 2017

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;

}

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 ,
Dec 19, 2017 Dec 19, 2017
LATEST

Thanks! Just what I needed. Much appreciated.

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