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

Conditional required fields based on a response in a dropdown box

New Here ,
Aug 31, 2017 Aug 31, 2017

I am in need of assistance creating a javascript that will set fields to required based on a response in a drop down field. 

I have an "Action" dropdown field where the user selects the action the want "Add", "Delete", "Change", "Other".  In the event of "Add" selected I need certain fields required, in the event "Delete" is selected I need fewer fields to appear as required and so on.

I have seen some similar javascripts for radial button responses but have had difficulty translating.

TOPICS
PDF forms
20.4K
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
New Here ,
Apr 22, 2023 Apr 22, 2023

Thank you so much. I will try this script.

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 ,
Apr 22, 2023 Apr 22, 2023

His descriptions are not very clear, he sent me a PM and I tried to give him answer but not heard back from him.

I don't think 'Advance' is a dropdown field, I think he wants to show result of fields1-6 in 'Advance' field if dropdown is "Yes" otherwise 'Advance' is 0, something like this:

var drop = this.getField("Dropdown").valueAsString;
if(drop == "No")
event.value = 0;
else
event.value = Number(this.getField("Field 6").valueAsString)+Number(this.getField("Total").valueAsString);

 

Where 'Total' is already the sum of fields 1-5 (at least that what I get from the description).

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
New Here ,
Apr 22, 2023 Apr 22, 2023

My apology for not getting back to you Nesa. I must have missed your reply. Anyway, this is the script that I wrote based on your collective input and it works.

 

var t1 = this.getField("Advance Request").valueAsString;
var t2 = this.getField("Registration Fee").value;
var t3 = this.getField("Lodging").value;
var t4 = this.getField("Total MIE").value;
var t5 = this.getField("Personal Car").value;
var t6 = this.getField("Parking").value;
var t7 = this.getField("Other").value;
if (t1=="Yes") event.value = t2+t3+t4+t5+t6+t7
else event.value = "0";

 

 

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 ,
Oct 19, 2023 Oct 19, 2023

Hello all, thanks for the helpful tips, but running into some issues with multiple conditional formatting. I am trying to have a conditional form fill where if you select a drop down item, it forces some fields to be required, and if you select other fields it simply is not required. I have followed the JS in these chats, but still do not require certain fields to be required properly. Also, when preparing the form, should the box for Required be checked or not on Properties screen for that each of these fields, or is the script essentially supposed to handle that automatically - I have tried both ways and no luck?

 

Goal is:

  1. IF Default - Bill Pay Check (3-7 Business Days) is selected, it does not require any of the f1-f5 fields to be filled in. 
  2. IF Expedited - Bill Pay ACH (2-3 Business Days) is selected, it does require all f1-f5 fields to be filled in
  3. IF Express - In House Wire (2 Business Days) is selected, it does require all f1-f5 fields to be filled in
  4. IF Overnight - In House Check and FedEx (1-2 Business Days) is selected, it does not require any of f1-f5 fields to be filled in. 

 

I thought I have the correct if/then statements below but It's definitely not functioning how I want.

 

Here's my script

var f1 = this.getField("Bank Name");
 
var f2 = this.getField("Account Name (Type)");
 
var f3 = this.getField("Account Number");
 
var f4 = this.getField("ABA Routing Wire");
 
var f5 = this.getField("ABA Routing ACH");
 
else if (event.value=="Default - Bill Pay Check (3-7 Business Days)") {
 
    f1.readonly = true;
 
    f2.readonly = true;
 
    f3.readonly = true;
 
    f4.readonly = true;
 
    f5.readonly = true;
 
    f1.required = false;
 
    f2.required = false;
 
    f3.required = false;
 
    f4.required = false;
 
    f5.required = false;
 
} else if (event.value=="Expedited - Bill Pay ACH (2-3 Business Days)") {
 
    f1.readonly = false;
 
    f2.readonly = false;
 
    f3.readonly = false;
 
    f4.readonly = false;
 
    f5.readonly = false;
 
    f1.required = true;
 
    f2.required = true;
 
    f3.required = true;
 
    f4.required = true;
 
    f5.required = true;
 
} else if (event.value=="Express - In House Wire (2 Business Days)") {
 
    f1.readonly = false;
 
    f2.readonly = false;
 
    f3.readonly = false;
 
    f4.readonly = false;
 
    f5.readonly = false;
 
    f1.required = true;
 
    f2.required = true;
 
    f3.required = true;
 
    f4.required = true;
 
    f5.required = true;
 
 
} else if (event.value=="Overnight - In House Check and FedEx (1-2 Business Days)") {
 
    f1.readonly = true;
 
    f2.readonly = true;
 
    f3.readonly = true;
 
    f4.readonly = true;
 
    f5.readonly = true;
 
    f1.required = false;
 
    f2.required = false;
 
    f3.required = false;
 
    f4.required = false;
 
    f5.required = false;
}
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 ,
Oct 19, 2023 Oct 19, 2023

You can't start with 'else if' so change first 'else if' into '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 ,
Oct 23, 2023 Oct 23, 2023

Thanks @Nesa Nurani - unfortunately I have changed the first line to "if" and it's still not conditionally requiring fields to be filled out, or not required based on the needs. Here's the updated script:

 

var f1 = this.getField("Bank Name");

var f2 = this.getField("Account Name (Type)");

var f3 = this.getField("Account Number");

var f4 = this.getField("ABA Routing Wire");

var f5 = this.getField("ABA Routing ACH");

if (event.value=="Default - Bill Pay Check (3-7 Business Days)") {

f1.readonly = true;

f2.readonly = true;

f3.readonly = true;

f4.readonly = true;

f5.readonly = true;

f1.required = false;

f2.required = false;

f3.required = false;

f4.required = false;

f5.required = false;

} else if (event.value=="Expedited - Bill Pay ACH (2-3 Business Days)") {

f1.readonly = false;

f2.readonly = false;

f3.readonly = false;

f4.readonly = false;

f5.readonly = false;

f1.required = true;

f2.required = true;

f3.required = true;

f4.required = true;

f5.required = true;

} else if (event.value=="Express - In House Wire (2 Business Days)") {

f1.readonly = false;

f2.readonly = false;

f3.readonly = false;

f4.readonly = false;

f5.readonly = false;

f1.required = true;

f2.required = true;

f3.required = true;

f4.required = true;

f5.required = true;


} else if (event.value=="Overnight - In House Check and FedEx (1-2 Business Days)") {

f1.readonly = true;

f2.readonly = true;

f3.readonly = true;

f4.readonly = true;

f5.readonly = true;

f1.required = false;

f2.required = false;

f3.required = false;

f4.required = false;

f5.required = false;
}

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 ,
Oct 23, 2023 Oct 23, 2023

The script works fine for me.

Check that your field names are correct.

Check console for errors (CTRL+J on Windows).

Share your file with, so we can see what is going on.

Since you have same conditions, you can reduce your script like this:

var f1 = this.getField("Bank Name");
var f2 = this.getField("Account Name (Type)");
var f3 = this.getField("Account Number");
var f4 = this.getField("ABA Routing Wire");
var f5 = this.getField("ABA Routing ACH");

if (event.value=="Default - Bill Pay Check (3-7 Business Days)" || event.value=="Overnight - In House Check and FedEx (1-2 Business Days)") {
f1.readonly = true;
f2.readonly = true;
f3.readonly = true;
f4.readonly = true;
f5.readonly = true;
f1.required = false;
f2.required = false;
f3.required = false;
f4.required = false;
f5.required = false;} 

else if (event.value=="Expedited - Bill Pay ACH (2-3 Business Days)" || event.value=="Express - In House Wire (2 Business Days)") {
f1.readonly = false;
f2.readonly = false;
f3.readonly = false;
f4.readonly = false;
f5.readonly = false;
f1.required = true;
f2.required = true;
f3.required = true;
f4.required = true;
f5.required = true;}

 

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 ,
Oct 23, 2023 Oct 23, 2023

Thanks for shortening the code. Attached is the document, I have checked multiple times but maybe my eyes are missing something on the script and naming. Thank you for your help. 

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 ,
Oct 23, 2023 Oct 23, 2023

Put the script in dropdown field as 'Validation' script.

Go to dropdown field properties, select 'Validate' tab and click on 'Run custom validation script', click on 'Edit' and paste script inside.

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 ,
Oct 23, 2023 Oct 23, 2023
LATEST

Oh heck yes! 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