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

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

Community Beginner ,
May 22, 2020 May 22, 2020

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

5.6K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
May 22, 2020 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);

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 26, 2020 May 26, 2020

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...";

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 22, 2020 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.

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 22, 2020 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);

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 Beginner ,
May 22, 2020 May 22, 2020

You are super awesome. My heartiest thanks to you. The code worked like a charm!! Million thanks

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 Beginner ,
May 26, 2020 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!

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 26, 2020 May 26, 2020

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...";
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 Beginner ,
May 26, 2020 May 26, 2020

Thanks for your prompt response. I have only text and choice boxes on the form. Where do I insert the script?

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 26, 2020 May 26, 2020

As the custom Calculation script of your text field.

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 Beginner ,
May 26, 2020 May 26, 2020

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?

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 26, 2020 May 26, 2020

Hard to say without seeing the file, but the first step should be to check the JS Console (Ctrl+J) for error messages.

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 Beginner ,
May 26, 2020 May 26, 2020

Is it possible that I create 2 text fields and based on the condition above hide/unhide 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
Community Expert ,
May 26, 2020 May 26, 2020

Yes, but it's not needed.

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 26, 2020 May 26, 2020

My guess is one of the names of the fields is incorrect, or their export values.

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 Beginner ,
May 26, 2020 May 26, 2020
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 26, 2020 May 26, 2020

Did you check the JS Console, as I've suggested? The problem will be very clear if you do.

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 Beginner ,
May 28, 2020 May 28, 2020

Thanks. It worked, saw the error on the console, fixed it. May thanks again!!

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 Beginner ,
Jan 03, 2023 Jan 03, 2023
LATEST

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;

 

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