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

help with conditional statements: if button is checked, certain text fields are required

New Here ,
May 13, 2025 May 13, 2025

hello, I am a little new to javascript and need to make an interactive form. thanks in advance for any guidance on this.

 

I have four radio buttons (C, D, E, and F) and depending which button is selected, certain text fields will be required by the user.  bascially:

If C == true, then Section 2 and 3 are required fields.

If E == true, then Section 1, 3, and 4 are required fields.

If D == true, then Section 3 is the required field.

If F == true, then Section 1, 3, and 4 are required fields. //same as E

 

After some reading about similar-ish issues, it sounds like the confitional statements need to be in the 4 different text fields. I've tried various codes that dont work. I have started with the first text field (Section1) in the validate tab with: 

 

var C = this.getField("Concept"); //does this.getField work with radio buttons?

var D = this.getField("Development");

var E = this.getField("Engineering");

var F = this.getField("Flight");

 

if(C==true){

this.getField("Section1").required = false

}

if(D==true){

this.getField("Section1").required = false

}

if(E==true){

this.getField("Section1").required = true

}

if(F==true){

this.getField("Section1").required = true

}

 

TOPICS
JavaScript , PDF forms
1.1K
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
1 ACCEPTED SOLUTION
Community Expert ,
May 14, 2025 May 14, 2025

Your "if" statement is the same as the switch/case statement in my code. It uses the export value of the "Status" radio buttons to determine the required state of the other fields.  

 

 

var cStatusValue = this.getField("Status").valueAsString; 
var oSectFields = this.getField("Section1");
switch(cStatusValue ){
     case "Concept":
        oSectFields .required = false;
        break;
     case "Development":
        oSectFields .required = false;
        break;
     case "Engineering":
        oSectFields .required = true;
        break;
     case "Flight":
        oSectFields .required = true;
        break;
     default:
        oSectFields .required = false;
        break;
}

 

Or you could write it like this:

var cStatusValue = this.getField("Status").valueAsString; 
var oSectFields = this.getField("Section1");
if(cStatusValue == "Concept")
    oSectFields.required = false;
else if(cStatusValue == "Development")
    oSectFields.required = false;
else if(cStatusValue == "Engineering")
    oSectFields.required = true;
else if(cStatusValue == "Flight")
    oSectFields.required = true;
else 
    oSectFields.required = false;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

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 ,
May 13, 2025 May 13, 2025

Your variables refer to the field objects so the field conditions will never equal true.  You should be testing the value of the fields.  Are these radio buttons or check boxes?  If they are radio buttons, how does the user unselect them?

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 ,
May 13, 2025 May 13, 2025

Yes, the C, D, E, and F are radio buttons.  I want the user to select one, either C, D, E or F, and depening on what button they select, certaion text fields (Sections) are required by the user. I am not sure I understand how the user can unselect the radio button... by clicking it? (thanks for your time!)

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 ,
May 13, 2025 May 13, 2025

Please read this article on using Radio buttons in a script:

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

In order for radio button to work together, they must have the same field name, but different export (or Choice) values. 

The script also needs to be places into the calculation script for one of the text fields that will be modified, or into a hidden text field that is added to the form just for this purpose, and isn't used anywhere else:

Here's a demo script:

var cRadioSelect = this.getField("RadioButt"); // replace with real radio button name
switch(cRadioSelect){
     case "Concept":
        this.getField("Section1").required = false;
        break;
     case "Development":
        this.getField("Section1").required = false;
        break;
     case "Engineering":
        this.getField("Section1").required = true;
        break;
     case "Flight":
        this.getField("Section1").required = true;
        break;
}

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 13, 2025 May 13, 2025

Thanks, Thom.  I have seen you help several folks with very similar issues. I have been looking at them most of today and am still quite stuck.

 

By the "same field name" do you mean the group? All four radio buttons (Concept, Development, Engineering, and Flight) are grouped together (called "Status"). since they are not checkboxes, I figured I could give the buttons different names. 

 

simple clarifcation questions so i can better understand:  cRadioSelect -- is "c" represent "case"? 

 

lastly, what is the difference if this code goes into the Actions tab of the Radio Button "Status" properties or having this code in the three different text fields (Section1, 2, and 3)? 

 

 

So, if I have multiple radio buttons in one field, i.e,: select one: C, D, E, or F  and they are in one group (Status), what Radio button name would I use here for the cRadioSelect?  Also, if a user needs to unselect a button, 

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 ,
May 13, 2025 May 13, 2025

Sasha314159265_0-1747175873476.png

If this helps...

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 ,
May 13, 2025 May 13, 2025

The "Group" name for a radio button is the same as the field name, so the name of your radio button fields is "Status". this is the name used in the "getField" function. Please read the article I posted. 

 

The "c" prefix is only there to indicate that the variable is a string. 

 

If you used the "MouseUp" action for the script on the radio button fields, then you would need to place individual code on each button. But if the code is placed in the calculation script for a single text field, then the code only needs to be on one place, because it can set the properties of all fields that need to be set. 

 

Calculation scripts are called any time any field on the form is changed. That means that it doesn't matter in which field the code is placed,  it could be any text field on the form, because it will always be called when any radio button is pressed.  

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 14, 2025 May 14, 2025

Thanks, Thom. I re-read the article more carefully and it helped my understand the grouped radio buttons better. however it does not help with understanding the logic to your demo code.  Since this is a similar (or so I thought) problem you have helped others with -- how to make certain text fields required based on radio button selection -- but your demo code that uses cRadioSelect, switch (cRadioSelect), and cases which were not implemented in other solutions. 

i understand the code can go in two different places for the same result: the buttons themselves or the conditional text field, but why does it have to be on each button and not applied to the radio button group?

 

This is another attempt in my miniature code skills to write up some logic for what I want the script to do. 

 

if(event.value == "") // If field is empty, other fields not required

{

   this.getField("Flight").required = true; //if Flight button is clicked

  ... set Section1 =  required

}

else

   this.getField("Demonstrated").required = true;

  ... set other fields ...

}

 

 

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 ,
May 14, 2025 May 14, 2025

I just tried something like this and I was successfully able to have Section1 text field required if Flight button is clicked. Now I just need to make this for the other three button conditions. How would I add the other three buttons, as separate variables? 

 

var S = this.getField("Status").valueAsString;
var T = this.getField("Section1");
if(S == "Flight")
T.required = true;
else
T.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 ,
May 14, 2025 May 14, 2025

Your "if" statement is the same as the switch/case statement in my code. It uses the export value of the "Status" radio buttons to determine the required state of the other fields.  

 

 

var cStatusValue = this.getField("Status").valueAsString; 
var oSectFields = this.getField("Section1");
switch(cStatusValue ){
     case "Concept":
        oSectFields .required = false;
        break;
     case "Development":
        oSectFields .required = false;
        break;
     case "Engineering":
        oSectFields .required = true;
        break;
     case "Flight":
        oSectFields .required = true;
        break;
     default:
        oSectFields .required = false;
        break;
}

 

Or you could write it like this:

var cStatusValue = this.getField("Status").valueAsString; 
var oSectFields = this.getField("Section1");
if(cStatusValue == "Concept")
    oSectFields.required = false;
else if(cStatusValue == "Development")
    oSectFields.required = false;
else if(cStatusValue == "Engineering")
    oSectFields.required = true;
else if(cStatusValue == "Flight")
    oSectFields.required = true;
else 
    oSectFields.required = false;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 21, 2025 May 21, 2025
LATEST

Thanks, Thom. This worked and I have learnt quite a bit. I am continuing my Javascript understanding so you will likely see my name again. Cheers!

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