Skip to main content
LoganWood
Participating Frequently
November 9, 2023
Question

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

  • November 9, 2023
  • 3 replies
  • 761 views

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!

This topic has been closed for replies.

3 replies

Nesa Nurani
Community Expert
Community Expert
November 10, 2023

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];

 

Thom Parker
Community Expert
Community Expert
November 9, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
November 9, 2023

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