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

Looping Through Specific Array of Fields and Checking Box Based on Field Values

Participant ,
May 12, 2025 May 12, 2025

I have an array of 30 fields "Price01 - Price30" and a checkbox. I am trying to loop through each field to see if it is greater than $100. If at least one of the 30 fields has a value greater than 100, I want the box to be checked. If not, then the box is unchecked.

So far I have the following validation script in each of the fields because checkboxes don't allow for validation scripts

var a = this.getField("Price01").valueAsString;
var b = this.getField("Price02").valueAsString;
var c = this.getField("Price03").valueAsString;
var d = this.getField("Price04").valueAsString;
var e = this.getField("Price05").valueAsString;

if (a >= "100" || b >= "100" || c >= "100" || d >= "100" || e >= "100")
{
		this.getField("TestBox").checkThisBox(0,event.value < 100 ? false : true);
    }


else {
    
	}
TOPICS
Create PDFs , How to , JavaScript , Modern Acrobat , PDF , PDF forms
305
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
1 ACCEPTED SOLUTION
Community Expert ,
May 12, 2025 May 12, 2025

Use this code, then:

 

var checkboxField = this.getField("TestBox");
var checkField = false;
for (var i=1; i<=30; i++) {
	var num = (i < 10 ? "0" + i : i);
	var price = Number(this.getField("Price" + num).valueAsString);
	if (price>100) {checkField = true; break;}
}
checkboxField.checkThisBox(0, checkField);

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 12, 2025 May 12, 2025

Name your checkboxes "TestBox01 - TestBox30" then place this script as custom calculation script in one of the text fields (you can remove all other scripts):

for (var i=1; i<=30; i++) {
 var num = (i < 10 ? "0" + i : i);
 var priceField = Number(this.getField("Price" + num).valueAsString);
 var checkboxField = this.getField("TestBox" + num);

 checkboxField.checkThisBox(0, (priceField > 100)? true : 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
Participant ,
May 12, 2025 May 12, 2025

Sorry.  Just to clarify, there is only one "TestBox" and 30 "Price" fields.

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 12, 2025 May 12, 2025

Use this code, then:

 

var checkboxField = this.getField("TestBox");
var checkField = false;
for (var i=1; i<=30; i++) {
	var num = (i < 10 ? "0" + i : i);
	var price = Number(this.getField("Price" + num).valueAsString);
	if (price>100) {checkField = true; break;}
}
checkboxField.checkThisBox(0, checkField);
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
Participant ,
May 13, 2025 May 13, 2025

It worked!  Thank you sir!

 

Just for my education, 1) why do we use calculation vs. validation and 2) why can we just put it into one field versus every field in the loop?

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 13, 2025 May 13, 2025

The answer to 1 and 2 is that the Calculation script is triggered each time the value of any field in the file is changed, while Validation is only triggered when the field it's associated with is edited. So instead of using 30 scripts, one for each Price field, we use only one. The downside of it is that it also executes when completely unrelated fields are edited, but unless you have a lot of calculations in your file you shouldn't notice that at all, and it can also be solved by adding a condition to the code that checks which field triggered the script, but that's a bit more advanced.

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
Participant ,
May 13, 2025 May 13, 2025

Ahhhh okay.

 

"Calculation script is triggered each time the value of any field in the file is changed".  I changed the value of some other fields but nothing was triggered?

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 13, 2025 May 13, 2025
LATEST

How do you know it wasn't triggered?

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 12, 2025 May 12, 2025

I see, sorry I misunderstood your question 🙂

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
Participant ,
May 13, 2025 May 13, 2025

No problem!  Thank you for your help 🙂

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