Skip to main content
Participating Frequently
May 22, 2020
Answered

How to write a Javascript code for making a field required conditionally, in a PDF Form?

  • May 22, 2020
  • 3 replies
  • 6472 views

I have designed an Interactive PDF Form in which the user is required to check 4 boxes eg A,B,C,D. If the user fails to check any of the 4 boxes, comments section has to be come a required field. This will require a Javascript I assume, which i don't know. Please help

    This topic has been closed for replies.
    Correct answer try67

    Use this code as the custom calculation script of your field:

     

    var a = this.getField("group 1").valueAsString=="No";
    var b = this.getField("group 2").valueAsString=="No";
    var c = this.getField("group 3").valueAsString=="No";
    
    if (a && b && c) event.value = "Thank you...";
    else event.value = "Sorry...";

    3 replies

    Known Participant
    January 3, 2023

    I would like a text box where numbers are entered and if a certain number is entered, a specific hidden Template will populate.  Can you please help me with the coding? This is what I have so far, but it will only populate the last template.  The first two aren't populating no matter what number I enter.

    var a = this.getField("Signing1-9").valueAsString=="1";
    var b = this.getField("Signing1-9").valueAsString=="2";
    var c = this.getField("Signing1-9").valueAsString=="3";
    var d = this.getField("Signing1-9").valueAsString=="4";
    var e = this.getField("Signing1-9").valueAsString=="5";
    var f = this.getField("Signing1-9").valueAsString=="6";
    var g = this.getField("Signing1-9").valueAsString=="7";
    var h = this.getField("Signing1-9").valueAsString=="8";
    var i = this.getField("Signing1-9").valueAsString=="9";
    if(a && b && c)event.value=this.getTemplateAsString("Regulatory").hidden=false;this.getTemplateAsString("Signing1-3").hidden=false;
    if(d && e && f)event.value=this.getTemplate("Signing4-6").hidden=false;
    else event.value=this.getTemplate("7-9").hidden=false;

     

    try67
    Community Expert
    May 22, 2020

    As the custom calculation script of the comments field enter the following code:

     

    var a = this.getField("A").valueAsString=="Off";
    var b = this.getField("B").valueAsString=="Off";
    var c = this.getField("C").valueAsString=="Off";
    var d = this.getField("D").valueAsString=="Off";

    event.target.required = (a || b || c || d);

    psa5Author
    Participating Frequently
    May 26, 2020

    Please help me with one more thing:

    I have 3 questions with Yes/No choice. I have created group 1, group 2 and group 3. If the answer for all the 3 is No then i have to display a message " Thank you ......" and if any one is Yes then the display message will be "Sorry ...". How do we display these messages? Thanks in advance!

    psa5Author
    Participating Frequently
    May 26, 2020

    As the custom Calculation script of your text field.


    I inserted a new text field inthe form and added the script to Custom Calculation script but after i select the choices for the groups it does not display the message in the text box. What am i missing? I have made it a read only field. Is it possible to display the message in this text box?

    Is it possible that I create 2 test fields and based on the condition above hide/unhide them?

    LinSims
    Community Expert
    May 22, 2020

    Let me move this to the Acrobat forum for you, where you are more likely to receive an answer to your question.

    The Community Help forum is for help in using the Adobe Support Community forums, not for help with specific programs. Product questions should be posted in the associated product community.