Skip to main content
Participating Frequently
March 20, 2025
Answered

Required Fields Based on Drop Down

  • March 20, 2025
  • 2 replies
  • 497 views
I'm creating a form where I have people registering for an event.
There is a drop down list for users to select 0-4 people in attendance. Below that, we have 4 spaces for each person attending.
I'm looking for a script that will only show the field and make it required based on the number of people.
 
Ex)
---User selects 0 (0 Value) people in attendance from the dropdown (labelled as FEG)
Fields "NameA", "NameB", "NameC", and "NameD" are hidden and not required.
 
---User selects 1 (225 Value) person in attendance from the dropdown (labelled as FEG)
Field "NameA" is made visible and required. Fields "NameB", "NameC", and "NameD" are hidden and not required.
 
---User selects 2 (450 Value) people in attendance from the dropdown (labelled as FEG)
Fields "NameA" and "NameB" are made visible and required. Fields , "NameC", and "NameD" are hidden and not required.
 
---User selects 3 (675 Value) people in attendance from the dropdown (labelled as FEG)
Fields "NameA", "NameB", and "NameC" are made visible and required. Fields "NameD" is hidden and not required.
 
---User selects 4 (900 Value) people in attendance from the dropdown (labelled as FEG)
Fields "NameA" and "NameB", "NameC", and "NameD" are made visible and required.
 
I've tried a few scripts, based off of other things I've seen, but it's not working out for me.
Correct answer try67

Use this code pattern. Place it as the custom Calculation script of "FEG":

 

if (event.value=="0") {
	this.getField("NameA").display = display.hidden;
	this.getField("NameA").required = false;
	this.getField("NameB").display = display.hidden;
	this.getField("NameB").required = false;
	this.getField("NameC").display = display.hidden;
	this.getField("NameC").required = false;
	this.getField("NameD").display = display.hidden;
	this.getField("NameD").required = false;
	this.resetForm(["NameA", "NameB", "NameC", "NameD"]);
} else if (event.value=="1") {
	this.getField("NameA").display = display.visible;
	this.getField("NameA").required = true;
	this.getField("NameB").display = display.hidden;
	this.getField("NameB").required = false;
	this.getField("NameC").display = display.hidden;
	this.getField("NameC").required = false;
	this.getField("NameD").display = display.hidden;
	this.getField("NameD").required = false;
	this.resetForm(["NameB", "NameC", "NameD"]);
} // etc.


I added to it a command to clear the hidden fields, so you don't get values that shouldn't be there when exporting the form data.

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 20, 2025

Use this code pattern. Place it as the custom Calculation script of "FEG":

 

if (event.value=="0") {
	this.getField("NameA").display = display.hidden;
	this.getField("NameA").required = false;
	this.getField("NameB").display = display.hidden;
	this.getField("NameB").required = false;
	this.getField("NameC").display = display.hidden;
	this.getField("NameC").required = false;
	this.getField("NameD").display = display.hidden;
	this.getField("NameD").required = false;
	this.resetForm(["NameA", "NameB", "NameC", "NameD"]);
} else if (event.value=="1") {
	this.getField("NameA").display = display.visible;
	this.getField("NameA").required = true;
	this.getField("NameB").display = display.hidden;
	this.getField("NameB").required = false;
	this.getField("NameC").display = display.hidden;
	this.getField("NameC").required = false;
	this.getField("NameD").display = display.hidden;
	this.getField("NameD").required = false;
	this.resetForm(["NameB", "NameC", "NameD"]);
} // etc.


I added to it a command to clear the hidden fields, so you don't get values that shouldn't be there when exporting the form data.

Participating Frequently
March 23, 2025

That works wonderfully!

Thank you so much! 

PDF Automation Station
Community Expert
Community Expert
March 20, 2025

Does 225 Value mean 225 is the export value for a selection of 1, and 450 is the export value for a selection of 2, etc.?

Participating Frequently
March 23, 2025

Yes, that is correct.