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

Conditional Required Fields

Explorer ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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

Views

3.1K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Mar 11, 2020 Mar 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

 

Votes

Translate

Translate
Community Expert ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Jul 22, 2024 Jul 22, 2024

Copy link to clipboard

Copied

LATEST

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

 

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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.

 

 

Votes

Translate

Translate

Report

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
Explorer ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Explorer ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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;
}

Votes

Translate

Translate

Report

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
Explorer ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

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 ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

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
Explorer ,
Mar 12, 2020 Mar 12, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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