Skip to main content
Participating Frequently
May 13, 2025
Answered

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

  • May 13, 2025
  • 1 reply
  • 1094 views

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

}

 

Correct answer Thom Parker

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;


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;

 

1 reply

PDF Automation Station
Community Expert
Community Expert
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?

Participating Frequently
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!)

Thom Parker
Community Expert
Community Expert
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 PDFScriptingUse the Acrobat JavaScript Reference early and often