Skip to main content
Known Participant
May 1, 2025
Answered

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

  • May 1, 2025
  • 2 replies
  • 615 views

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.

Correct answer PDF Automation Station

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;
}

 

2 replies

PDF Automation Station
Community Expert
Community Expert
May 1, 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;
}

 

Thom Parker
Community Expert
Community Expert
May 1, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
May 1, 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.

PDF Automation Station
Community Expert
Community Expert
May 1, 2025

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