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

How can I loop through specific text fields and not the whole page?

Community Beginner ,
May 07, 2023 May 07, 2023

I have a table of text fields separated by two columns, and I need to make sure the number of entries in one column matches a number entered in a text field on a separate page. 

 

So if I say numInColumn1: 4

There needs to be 4 text fields in the first column with something written in them, or else I throw an error. Is this possible? I would need to duplicate this logic for the second column. 

I don't have my exact code in front of me, but what I have tried goes like this:

 

total = 0;

for (i = 1; i < numFields; i++) {
  var field = this.getField("Column1Row" + i);

  if (/\w/.test(field)) {

    total++;

  }
}
if (this.getField("numInColumn1") != total) {
  app.alert("value does not match the total of " + total);

  event.rc = false;

 

 

All of the errors I have gotten when tweaking this in different ways is the program either counting all of the fields in the pdf file or not getting an error message at all. Any help is appreciated. 

TOPICS
General troubleshooting , JavaScript , PDF , PDF forms
3.1K
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 07, 2023 May 07, 2023

EDIT:

If you use script in "numInColumn1" field, then use it in 'Validate', change 10 to number of fields you have in column:

 

var total = 0;
for(var i=1; i<=10; i++){
if(this.getField("Column1Row"+i).valueAsString != "")total++;}

if(event.value != total) {
app.alert("value does not match the total of " + total);
event.rc = 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
Community Beginner ,
May 07, 2023 May 07, 2023

This makes sense. The reason I used numFields is because the table is part of a template that can be added indefinitely, so I wanted to account for any number of columns available. 

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 07, 2023 May 07, 2023

If you will be adding new fields to the column, try using like this:

var t = 0;
for(var j = 0; j < this.numFields; j++){
if(/^Column1Row/g.test(this.getNthFieldName(j)))t++;}

var total = 0;
for(var i=1; i<=t; i++){
if(this.getField("Column1Row"+i).valueAsString != "")total++;}

if(event.value != total) {
app.alert("value does not match the total of " + total);
event.rc = false;}

First part will check if field name contain "Column1Row" and it will set it in loop correct number of fields.

In the future it would be better to just group fields like this "Column1Row.1" for easier scripting.

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 11, 2023 May 11, 2023

Thank you! I made the adjustment to the line

if(/^Column1Row/g.test(this.getNthFieldName(j)))t++}

and took out the ^, so it can account for all of the dynamically changing variables in any pages added to the form (Ex. I believe it changes to P4.Extra_page.Column1Row[number]).

 

I have one more question: Can I take that same regular expression you used to check if the field name contains Column1Row and insert that into "this.getField().valueAsString"? That way I would be able to run the logic on all of the field names in any table with just a single for-loop.

 

I tried to do something like this, but no luck:

var columnRows = /Column1Row/g.test(this.getNthFieldName(j));
if (this.getField(columnRows).valueAsString != "") t++;

 

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 11, 2023 May 11, 2023

You didn't declare variable j.

To check for other columns, try something like this: /Column.*Row/g

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

Sorry to have so many questions. Can this be used inside of this.getField? 

so "this.getField(/Column.*Row/g)"

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

No.

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

You can put all fields in an array:

var fields = [];
for(var j = 0; j < this.numFields; j++){
if(/Column.*Row/g.test(this.getNthFieldName(j)))
fields.push(this.getNthFieldName(j));}

then loop over the array and do what you wish with it.

 

 

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 12, 2023 May 12, 2023
LATEST

Thanks. You've been a great 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