Skip to main content
Known Participant
April 4, 2018
Answered

Extract a First Name from a field containing a Full Name

  • April 4, 2018
  • 2 replies
  • 4744 views

Hi there, I have been searching everywhere and can't find a script for what I need.

I have a text field containing a full name, I need to extract just the first name.  The names are simply separated with a space, but how do I get just the first name?

I have a text box where a user types in the full name eg. John Michael Smith.  I need to create a custom text box to display just the first name of John.

Thanks.

Phil

This topic has been closed for replies.
Correct answer try67

OK, sure. As the custom calculation script of "First.Name" enter this code:

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

2 replies

Known Participant
April 5, 2018

I created a text field, went to text field properties, format, custom category and pasted the code in the custom format script box.  Do I need to change anything else or add an action or anything?

try67
Community Expert
Community Expert
April 5, 2018

Why under Format? I wrote that this is a custom calculation​ script.

Known Participant
April 5, 2018

OMG, fantastic.  It's working perfectly.  Just goes to show when someone tries to teach you something who doesn't know what they're talking about in the first place.

Thank you so much.

Phil

try67
Community Expert
Community Expert
April 4, 2018

It's quite easy to extract the first word from a string, but you should be careful doing it. Many people have more than one first name and will not like it if you assumed their name is something it isn't. Same goes for last names. I always recommend to do it the other way around. Have people enter their first and last names separately and then combine them to the full name automatically.

Known Participant
April 5, 2018

Hi, thanks for your reply.  I actually already have two fields, "First Names" and "Surname"  I have a calculated field to combine the two, but on one form, I specifically need the first name only, so if it's John Michael James Smith, I need to extract John to place in a separate field.

My field names are obviously First.Names and First.Name, can you help?

Thanks.

Phil 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 5, 2018

OK, sure. As the custom calculation script of "First.Name" enter this code:

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