Conditional required fields based on a response in a dropdown box

New Here ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

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, Windows

Views

2.8K

Likes

Translate

Translate

Report

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
Explorer ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

I'm trying to develop something very similar: for the drop down, if one selects "Yes, I am", two text fields (already visible) will become required, and a third will become read-only.

If the selection is "No, I am not," the third text field will be required and the other two text fields will become read-only.

The dropdown selection-to-required field relationship seems very specific, as I haven't been able to adapt any of the solutions for other box types.

Thanks!

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

Use this code as the custom validation script of the drop-down field (adjust the field names as needed):

var f1 = this.getField("Field1");

var f2 = this.getField("Field2");

var f3 = this.getField("Field3");

if (event.value=="Yes, I am") {

    f1.readonly = false;

    f2.readonly = false;

    f3.readonly = true;

    f1.required = true;

    f2.required = true;

    f3.required = false;

} else if (event.value=="No, I am not") {

    f1.readonly = true;

    f2.readonly = true;

    f3.readonly = false;

    f1.required = false;

    f2.required = false;

    f3.required = true;

}

Make sure to tick the option to commit the selected value of the drop-down immediately (under the field's Properties, Options tab).

Likes

Translate

Translate

Report

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
Explorer ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

Thank you so much! I made some adjustments for some additional fields and functionality and it worked perfectly. Thank you for answering so quickly!

var f1 = this.getField("SuperPro");
var f2 = this.getField("SupervisingProfessionalName");
var f3 = this.getField("SPNumber");
var f4 = this.getField("Initial");
if (event.value=="select one") {
    f1.readonly = false;
    f2.readonly = true;
    f3.readonly = true;
    f4.readonly = true;
    f1.required = false;
    f2.required = false;
    f3.required = false;
    f4.required = false;
}
if (event.value=="Yes, I am") {
    f1.readonly = false;
    f2.readonly = false;
    f3.readonly = false;
    f4.readonly = true;
    f1.required = false;
    f2.required = true;
    f3.required = true;
    f4.required = false;
} else if (event.value=="No, I am not") {
    f1.readonly = false;
    f2.readonly = true;
    f3.readonly = true;
    f4.readonly = false;
    f1.required = false;
    f2.required = false;
    f3.required = false;
    f4.required = true;
}

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

You should change this line:

if (event.value=="Yes, I am") {

To:

else if (event.value=="Yes, I am") {

But for the rest it looks pretty good!

Likes

Translate

Translate

Report

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
Explorer ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

In the same form, I have one group of two radio buttons. Selecting the first radio button (RB1) does not trigger any event. Selecting the second radio button (RB2) will make a subsequent Dropdown list box a "Required' field.

When selecting RB2, the Dropdown that follows becomes "Required"; however, if you change your mind and select RB1 instead, the Dropdown stays in "required" status (and can only be manually set to "optional.")

I've included this in the "custom calculation" for the Dropdown:

var v = this.getField("CPD not met").value; //RB2

var f = this.getField("CPT not met reason");  //Drop Down

var u = this.getField("CPD met"); //RB1

if (v=="Yes") {f.required = true;}

else if (u=="Yes") {f.required=false;} //this line added afterwards to try to make Dropdown optional again
else {f.required = false;}

Am I close?

Thanks!

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

The second if-condition is not needed. All you need is the condition to set it as required, and everything else should cause it to not be required.

Likes

Translate

Translate

Report

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 ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

I know nothing about javascript so please excuse my ignorance on the matter. but I am trying to write JavaScript in Adobe Pro DC and it's something similar to what the person above is wanting. I have a field that is a drop down and I want another field to be required when the drop down answer is yes.

When I look at the script above, is the "drop down" field represented in any of the var f1, var f2 or var f3 fields? In other words, how does the event.value get assigned to the proper field?

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

event.value means the value of field where you put code, e.g. if you put code in dropdown field event.value means the value of that dropdown field.

Put this code in "Validation" of dropdown field and check "Commit selected value immediately" in dropdown options tab:

this.getField("Text1").required = event.value == "Yes" ? true : false;

Change "Text1" to the name of field you want to make required.

Likes

Translate

Translate

Report

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 ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

ok. I was think code was assigned to the entire document. How do I assign
it to a particular field?

--
Jerri Jones
Database Assistant Manager
Excelsior Springs School District #40

--










EXCELSIOR SPRINGS 40 SCHOOL DISTRICT CONFIDENTIALITY NOTICE: This
electronic mail transmission (including any accompanying attachments) is
intended solely for its authorized recipients(s), and may contain
confidential and/or legally privileged information. If you are not an
intended recipient, or responsible for delivering some or all of this
transmission to an intended recipient, be aware that any review, copying,
printing, distribution, use or disclosure of the contents of this message
is strictly prohibited. If you have received this electronic mail message
in error, please delete it from your system without copying it, and contact
the sender immediately.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

First select "Prepare form" tool, then right click on dropdown field and select "Properties" from pop up window.
1) Select "Validate" tab.
2) Select "Run custom validation script".
3) Click "Edit" and paste code inside.

tempsnip.png

Likes

Translate

Translate

Report

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 ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

I don't see a "validate" tab when I click on properties.- Perhaps I'm using
a different version of the program or something?

[image: image.png]

--
Jerri Jones
Database Assistant Manager
Excelsior Springs School District #40

--










EXCELSIOR SPRINGS 40 SCHOOL DISTRICT CONFIDENTIALITY NOTICE: This
electronic mail transmission (including any accompanying attachments) is
intended solely for its authorized recipients(s), and may contain
confidential and/or legally privileged information. If you are not an
intended recipient, or responsible for delivering some or all of this
transmission to an intended recipient, be aware that any review, copying,
printing, distribution, use or disclosure of the contents of this message
is strictly prohibited. If you have received this electronic mail message
in error, please delete it from your system without copying it, and contact
the sender immediately.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

LATEST

Your image did not come through. You can't attach images when replying by email. Please reply directly from the forum.

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

What do you mean? It only applies to the field you specify. The document itself can't be set as "required"...

Likes

Translate

Translate

Report

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