Split String to Extract First Word from Text field

New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I would like to split a text field, named Client1, to extract just the first word using a space as the delimiter.

 

Here is what I've put together from reasearch, but it isn't working when placed as a custom calculation script in a read only text field:

 

var str1 = this.getField("Client1").value;
var res1 = str1.split(" ", 1);

event.value = res1

 

I think I'm missing something in going from the array produced by var res1 to the desired event.value for the field. I appreciate a point in the right direction - thanks!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

I suspected it was a name field... In that case I would recommend you don't do that, as you can't know how long the actual first name is (many people have multiple words in their names, both first and last, not to mention middle names, titles, etc.). Let the user enter their own name. If you want to make things a bit more automated then do it the other way around: Let the user enter their first and last names separately and then combine them automatically to a "Full Name" field.

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

259

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Split String to Extract First Word from Text field

New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I would like to split a text field, named Client1, to extract just the first word using a space as the delimiter.

 

Here is what I've put together from reasearch, but it isn't working when placed as a custom calculation script in a read only text field:

 

var str1 = this.getField("Client1").value;
var res1 = str1.split(" ", 1);

event.value = res1

 

I think I'm missing something in going from the array produced by var res1 to the desired event.value for the field. I appreciate a point in the right direction - thanks!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

I suspected it was a name field... In that case I would recommend you don't do that, as you can't know how long the actual first name is (many people have multiple words in their names, both first and last, not to mention middle names, titles, etc.). Let the user enter their own name. If you want to make things a bit more automated then do it the other way around: Let the user enter their first and last names separately and then combine them automatically to a "Full Name" field.

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

260

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

I think I got it to work with this:

 

var str1 = this.getField("Client1").value;
var res1 = str1.split(" ");

if (res1.length==2){
var first = res1[0];
var last = res1[1];

event.value = first;
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Most Valuable Participant ,
Apr 23, 2020

Copy link to clipboard

Copied

If the value of this field will be more (or less!) than two words your script won't work.

You should remove that if-condition.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
New Here ,
Apr 23, 2020

Copy link to clipboard

Copied

Hiii!  It's a full name field and I want to extract the first name - so there is the possibility of the field being more than two words - so just use this?

 

var str1 = this.getField("Client1").value;
var res1 = str1.split(" ");

var first = res1[0];

event.value = first;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2020 0
Most Valuable Participant ,
Apr 24, 2020

Copy link to clipboard

Copied

I suspected it was a name field... In that case I would recommend you don't do that, as you can't know how long the actual first name is (many people have multiple words in their names, both first and last, not to mention middle names, titles, etc.). Let the user enter their own name. If you want to make things a bit more automated then do it the other way around: Let the user enter their first and last names separately and then combine them automatically to a "Full Name" field.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2020 0
sd0123 LATEST
New Here ,
Apr 24, 2020

Copy link to clipboard

Copied

Gotcha! Thank you 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2020 0