Skip to main content
deckarduk
Inspiring
March 31, 2022
Question

Number of form fields with same name query

  • March 31, 2022
  • 2 replies
  • 2760 views

Hi there,

I've written a script to add checkboxes to a page and the code works ok.

I'm in the process of putting some error handling in however I've hit a slight niggle.

If I use the following to reference a formfield by name is there a quick way to check if there's more than one field with the same name?

thisField = this.getField(theBaseFieldName);

I thought I might be able to check thisField.length to see if it's greater than 1 but unfortunately not. I guessing it's because of the way I'm refering the object. Please can someone point me in the right direction?

Thanks in advance.

This topic has been closed for replies.

2 replies

deckarduk
deckardukAuthor
Inspiring
April 6, 2022

Hi there,

Please can someone point me in the right direction.

The code below doesn't work, or works intermittenly, yet I have verified there is only 1 form field with the name 'ewq'.

var theBaseField = 'ewq';
var thisField = this.getField(theBaseField);
var fieldsNum = thisField.page.length;
//app.alert(fieldsNum)


if (numFields==1) {
    app.alert('Message-1')
} else {
    app.alert('Message-2')
}

If I un-comment app.alert(fieldsNum) I get an undefined error yet the field is there on the page?

Could it be that a persistent session variable is causing the issue?

 

Thanks in advance.

Bernd Alheit
Community Expert
Community Expert
April 6, 2022

You have not set the variable numFields?

deckarduk
deckardukAuthor
Inspiring
April 6, 2022

Also, numFields is a name of a (read-only) property of the Document object, so it's not a good idea to define a variable with that same name, as the two can conflict.


Thanks for the help Bernd and Gilad.

 

I'm still getting undefined, and not 1, if I run the code below with only 1 form field named 'ewq123' ?

The pdf in question is 19 pages in length and has numerous form fields in it.

If I duplicate the form field, so there are 2 items named 'ewq123', the alert reports '2'.

I've even duped both of those, to create 4 items named the same, and the alert reports '4'. I've even added the form field again and duped it as above. All works fine until there is only 1 form field.

With only 1 form field, out of many, the alert reports undefined?

Here's the code again...

var theBaseField = 'ewq123';
var thisField = this.getField(theBaseField);
var num_of_fields = thisField.page.length;
app.alert(num_of_fields)

 Thanks again for the help.

Bernd Alheit
Community Expert
Community Expert
March 31, 2022

Check the property page of the field.

deckarduk
deckardukAuthor
Inspiring
April 1, 2022

Hi Bernd,

Thanks for your reply.

 

I'm not sure if I explained things correctly.

All I'm trying to do is find out how many checkboxes there are with the name 'xyz123'.

If the count is greater than 1 then I need to catch that.

Is it a case of looping through all the form fields in the document and check the name of each one?

 

Thanks Bernd

Bernd Alheit
Community Expert
Community Expert
April 1, 2022

Check thisField.page