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

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

Explorer ,
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

729

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 3 Correct answers

Community Expert , 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.

Votes

Translate

Translate
Community Expert , Sep 22, 2022 Sep 22, 2022

Sure.  Either:

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

Or:

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

 

Votes

Translate

Translate
Community Expert , Sep 22, 2022 Sep 22, 2022

Add 'i' for case insensitive.

Example form above code:

(/ham/i.test(t1))

Votes

Translate

Translate
Community Expert ,
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.

Votes

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
Explorer ,
Jun 09, 2022 Jun 09, 2022

Copy link to clipboard

Copied

Thank you very much TRY67!!  I really appreciate.

Votes

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?

Votes

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
Community Expert ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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

Votes

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!

Votes

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
Community Expert ,
Jun 18, 2022 Jun 18, 2022

Copy link to clipboard

Copied

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

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

Votes

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
Explorer ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Is there I way I can change this formula so the text I am searching for is not case sensative?

Votes

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
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Sure.  Either:

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

Or:

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

 

Votes

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
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

LATEST

Add 'i' for case insensitive.

Example form above code:

(/ham/i.test(t1))

Votes

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