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

How to extract a last name that has a prefix from a text field? (e.g., Van Gogh, , da Vinci)

Community Beginner ,
Nov 09, 2023 Nov 09, 2023

Hello!

 

I am creating an auto-fill PDF sheet that requires information to be repeatedly filled in various locations across the packet. It is to be filled in with client info, and in this specific case, their name.

 

I currently am using a text field "Full Name" where they enter first and last. I have a second text field "First Name" that extracts their first name and another for their last name using a field "Last Name." Their are various locations they would need to fill in their full name and then their first and last separately.

 

I am using this calculation script to extract their first and last to the separate fields I named above:

 

event.value = this.getField("First.Names").valueAsString.split(" ")[0];

 

I'm just wondering how I could use this script to also extract last names that include prefixes, so if their full name is Vincent van Gogh, the last name field will be populated with van Gogh instead of just Gogh.

 

Any help is appreciated! Thanks!

TOPICS
How to , PDF , PDF forms
579
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 ,
Nov 09, 2023 Nov 09, 2023

There's no way to do that with 100% accuracy. You'll be better off letting people enter their own names separately.

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 ,
Nov 09, 2023 Nov 09, 2023

So how exactly do you know the last name has a prefix when you look at a full name? Think about it. You need an AI for that.

Try67's suggestion is the correct answer. Make them enter the last name separately.  

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Nov 09, 2023 Nov 09, 2023
LATEST

All the above suggestion are correct, however if you really want to do it the way you describe it, and you think that will work for you, use this:

var str = this.getField("First.Names").valueAsString.split(" ");
if(str.length == 3)
event.value = str[1]+" "+str[2];
else if(str.length == 2)
event.value = str[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