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

Show text field depending on what is entered in another text field

Community Beginner ,
May 01, 2025 May 01, 2025

I have a text field called Name.  I have two other text fields called City1 and City2.  If the word Chicago is entered into the Name text field, I want the City1 text field to be visible and the City2 text field to be hidden.  If the word Detroit is entered into the Name text field, I want the City2 text field to be visible and the City1 text field to be hidden.  Any help is greatly appreciated.

TOPICS
PDF , PDF forms
310
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
May 01, 2025 May 01, 2025

What if neither Detroit or Chicago is entered into the Name field?  If you want the fields hidden in this case, and you want to ignore case sensitivity in Detroit and Chicago, enter the follow custom validation script in the Name field:

if(/chicago/i.test(event.value))
{
this.getField("City1").display = display.visible;
this.getField("City2").display = display.hidden;
}
else if(/detroit/i.test(event.value))
{
this.getField("City1").display = display.hidden;
this.getField("City2").display = display.visible;
}
else 
{
this.getField("City1").display = display.hidden;
this.getField("City2").display = display.hidden;
}

 

View solution in original post

Translate
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 ,
May 01, 2025 May 01, 2025

What happens when something else is entered into "Name"?

But here is some code to add to the "Name" Validation Script.  It hides both fields on non-matching text entry.

 

// Validation Script for Name field
if(event.value == "Chicago"){
    this.getField("City1").display = display.visible;
    this.getField("City2").display = display.hidden;
}
if(event.value == "Detroit"){
    this.getField("City1").display = display.hidden;
    this.getField("City2").display = display.visible;
}
else {
    this.getField("City1").display = display.hidden;
    this.getField("City2").display = display.hidden;
}

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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 Beginner ,
May 01, 2025 May 01, 2025

When I type Chicago in the Name field, both boxes are hidden.  But when I type Detroit in the Name field, it works correctly (City 1 is hidden and City2 is visible.

Translate
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 ,
May 01, 2025 May 01, 2025

Add the word else and a space in front of @Thom Parker 's 2nd "if".

Translate
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 Beginner ,
May 01, 2025 May 01, 2025
LATEST

Now it works.  Thank you!

Translate
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 ,
May 01, 2025 May 01, 2025

What if neither Detroit or Chicago is entered into the Name field?  If you want the fields hidden in this case, and you want to ignore case sensitivity in Detroit and Chicago, enter the follow custom validation script in the Name field:

if(/chicago/i.test(event.value))
{
this.getField("City1").display = display.visible;
this.getField("City2").display = display.hidden;
}
else if(/detroit/i.test(event.value))
{
this.getField("City1").display = display.hidden;
this.getField("City2").display = display.visible;
}
else 
{
this.getField("City1").display = display.hidden;
this.getField("City2").display = display.hidden;
}

 

Translate
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