Skip to main content
Inspiring
March 11, 2020
Answered

Conditional Required Fields

  • March 11, 2020
  • 4 replies
  • 4342 views

I would like to make 5 fields required to be filled in but only if a different field is filled in. For example 

 

if "Text5" has a value

then "Text18" "Text31" "Text32" "Text57" "Text70" are required

 

it also needs to include

if a value is input into "Text5", then removed or deleted

then "Text18" "Text31" "Text32" "Text57" "Text70" are no longer required

 

The removal function is because I have a print button checking for required fields, so it has to turn back off too for that to work.

 

Thanks

    This topic has been closed for replies.
    Correct answer Thom Parker

    Put this on the Validate script for Text5

     

    var bRequired = event.value != event.target.defaultValue;

    this.getField("Text18").required = bRequired;

    ...etc for the rest of the fields

     

    4 replies

    jlabs21Author
    Inspiring
    March 11, 2020

    Almost got it but it's backwards from the way I want. Can anyone help?

    var v = (event.value==null || event.value=="");

    var d = this.getField("Text18");
    var p = this.getField("Text57");
    var b = this.getField("Text70");
    var g = this.getField("Text31");
    var t = this.getField("Text32");

    d.required = v;
    p.required = v;
    b.required = v;
    g.required = v;
    t.required = v;

    Thom Parker
    Community Expert
    Community Expert
    March 11, 2020

    So what about the code I provided didn't work for you? The answer is already there.

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    jlabs21Author
    Inspiring
    March 12, 2020

    I must have made a mistake on it yesterday. I tried it again after your response and it worked just like it should. Thanks for the help

    jlabs21Author
    Inspiring
    March 11, 2020

    I found part of the answer and it works, but I need it to turn off the required field if the value of "Text5" goes back to being empty.

    var q = this.getField("Text5");

    var d = this.getField("Text18");
    var p = this.getField("Text57");
    var b = this.getField("Text70");
    var g = this.getField("Text31");
    var t = this.getField("Text32");

    console.println(q.value);

    d.required = false;
    p.required = false;
    b.required = false;
    g.required = false;
    t.required = false;


    if (this.getField("Text5") != null);

    {
    d.required = true;
    p.required = true;
    b.required = true;
    g.required = true;
    t.required = true;
    }

    Inspiring
    March 11, 2020

    You can use the "if( [some logical statement] ) else " statement of JavaScript to test if "Text5" has a none null string value or if a number is not zero or a null string. If the result of the test is true then you use a block of code to set fields  "Text18" "Text31" "Text32" "Text57" "Text70" to being required. If not true then set them to not required or the "required" property to false. I would make that an on "Blur" or exit action.

     

     

    jlabs21Author
    Inspiring
    March 11, 2020

    Guys thanks for the help, I should have clarified better to begin with but I understand very little Java script writing.

    Can you break it down Barney style for me?

    I've been copying and pasting from forums but don't have a good grasp writing the code yet.

    Thom Parker
    Community Expert
    Thom ParkerCommunity ExpertCorrect answer
    Community Expert
    March 11, 2020

    Put this on the Validate script for Text5

     

    var bRequired = event.value != event.target.defaultValue;

    this.getField("Text18").required = bRequired;

    ...etc for the rest of the fields

     

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Known Participant
    July 22, 2024

    Thank you SO much! I was just searching the community for answers because I had a similar issue. This is so easy!!!!