Script Needed - If Text Box contains specific word, another text box returns specific response

New Here ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Hi...  I am new to Java and Adobe. I searched posts and could not find the answer.

I am trying to create a document with fields that automatically populate specific responses.

Example:

If TextField1 equals "Apples", TextField2 populates "Fruit"

If TextField1 equals "Steak", TextField2 populates "Meat"

If TextField1 equals "Crackers" "Chips" or "Nuts", TextField2 populates "Snack"

Are there any Java geniuses out there that can help out a rookie?

TOPICS
JavaScript

Views

45

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 09, 2022 Jun 09, 2022
You can use this code as the custom calculation script of TextField2: var t1 = this.getField("TextField1").valueAsString; if (t1=="Apples") event.value = "Fruit"; else if (t1=="Steak") event.value = "Meat"; else if (t1=="Crackers" || t1=="Chips" || t1== "Nuts") event.value = "Snack"; else event.value = ""; PS. For future reference, this is JavaScript, not Java.

Likes

Translate

Translate
Adobe Community Professional ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

You can use this code as the custom calculation script of TextField2:

 

var t1 = this.getField("TextField1").valueAsString;
if (t1=="Apples") event.value = "Fruit";
else if (t1=="Steak") event.value = "Meat";
else if (t1=="Crackers" || t1=="Chips" || t1== "Nuts") event.value = "Snack";
else event.value = "";

 

PS. For future reference, this is JavaScript, not Java.

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
community guidelines
New Here ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Thank you very much TRY67!!  I really appreciate.

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
community guidelines
New Here ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

How would you modify this to look for a word in a sentence, for example, "My favourite foods are ham and steak.", how would you check to see if the word "ham" is within the string?

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
community guidelines
Adobe Community Professional ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

if (/ham/.test(t1)) ...

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
community guidelines
New Here ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

Thank you very much!

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
community guidelines
Adobe Community Professional ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

LATEST

If you're going to search for texts with punctuations, though, then use this method:

if (t1.indexOf("ham")!=-1) ...

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
community guidelines