Skip to main content
Participating Frequently
Mar 20, 2025 2:52 PM UTC
Answered

Required Fields Based on Drop Down

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.
try67
try67Correct answer
Community Expert
Mar 20, 2025 4:05 PM UTC

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.

4 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Mar 20, 2025 4:05 PM UTC

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.

Translate
Participating Frequently
Mar 23, 2025 12:24 PM UTC

That works wonderfully!

Thank you so much! 

Translate
PDF Automation Station
Community Expert
Community Expert
Mar 20, 2025 3:47 PM UTC

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.?

Translate
Participating Frequently
Mar 23, 2025 12:25 PM UTC

Yes, that is correct.

Translate