How can I loop through specific text fields and not the whole page?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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++;
Copy link to clipboard
Copied
You didn't declare variable j.
To check for other columns, try something like this: /Column.*Row/g
Copy link to clipboard
Copied
Sorry to have so many questions. Can this be used inside of this.getField?
so "this.getField(/Column.*Row/g)"
Copy link to clipboard
Copied
No.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks. You've been a great help

