Javascript: Getting the first number in Field.Page array

New Here ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

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");
    }
}
TOPICS
JavaScript , PDF forms

Views

38

Likes

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
Adobe Community Professional ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

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.

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

Screen Shot 2022-08-08 at 8.24.39 AM.pngScreen Shot 2022-08-08 at 8.23.52 AM.png

Likes

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
Adobe Community Professional ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

This happens when f.page is not an array.

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

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