Skip to main content
davidd39277856
Inspiring
August 5, 2022
Answered

Javascript: Getting the first number in Field.Page array

  • August 5, 2022
  • 2 replies
  • 1130 views

I'm working on a button script that will tell a user which required fields are yet to be filled-out.  I'm halfway there, and code is below.  The problem is when a field name is encountered on more than one page (which is common on my document, which is a collection of forms) it returns an array of the page numbers, such as "1, 2, 5".  I was thinking it would be less confusing to the users to just see mention of the first instance of each field's page number, and once filled-in, the others with the same field name would be filled-in as well.  Is there a way to get the first item in the Field.Page array?

 

var emptyFields = [];
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f.type != "button" && f.required && f.display!=display.hidden && f.valueAsString==f.defaultValue) {
        emptyFields.push("Page " + f.page);
        emptyFields.push(f.name, "\n");
    }
}
This topic has been closed for replies.
Correct answer davidd39277856

This happens when f.page is not an array.


Ah, thank you.  So I a test to handle array vs. non-array data, with appropriate script.  I'll work on that.

2 replies

davidd39277856
Inspiring
August 8, 2022

This appears to work.  I found a way to test if f.page was an array, and then to assign the value to g.

 

var emptyFields = [];
for (var i = 0; i < this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f.type != "button" && f.required && f.display!=display.hidden && f.valueAsString==f.defaultValue) {
 
        if (f.page.constructor !== Array){
          var g = f.page;
        } else var g = f.page[0];
 
        emptyFields.push("Page " + (g+1));  
        emptyFields.push(f.name, "\n");
        
    }
}
if (emptyFields.length > 0) {
    app.alert("You must fill in the following fields:\n\n" + emptyFields.join ("\n"));
}
Bernd Alheit
Community Expert
Community Expert
August 6, 2022

When f.page is an array use f.page[0]

try67
Community Expert
Community Expert
August 6, 2022

Note that that's not necessarily the lowest value, though, as the array is sorted by the index number of the widgets (basically, the order in which they were created), not by their locations.

davidd39277856
Inspiring
August 8, 2022

Thank you for the responses.  The initial problem is solved, but it does raise a secondary problem.  The alert box will report the first field's first page, but subsequent fields are undefined.  Also, when I add "1" to adjust the page number for the user, it returns "NaN."