Skip to main content
Participating Frequently
April 6, 2022
Answered

Page Validation

  • April 6, 2022
  • 1 reply
  • 1233 views

Hello! I am trying to add a button on each page of my PDF to validate all required fields on that page only. I have the below code, but this validates the required fields in the entire document. Anybody know how to make it page specific?

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required ) {

if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

}

}

if (emptyFields.length>0) {

app.alert("The following fields are required:\n" + emptyFields.join("\n"),1,0, 'Validation Error');

}
else app.execMenuItem("SaveAs");

This topic has been closed for replies.
Correct answer try67

If I want to just check a couple of fields instead of the entire document how do I do that? When i use the below i still get a popup even if the fields are filled out.

 

var emptyFields = [
"Textbox1",
"Checkbox1",
]
if (f.type!="button" && f.required ) {
if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
}
if (emptyFields.length>0)
{
app.alert("The following fields are required:\n" + emptyFields.join("\n"),1,0, 'Validation Error');
}
else app.execMenuItem("SaveAs");


No, not quite like that. Try this:

 

var fieldsToCheck = ["Textbox1", "Checkbox1"];
var emptyFields = [];
for (var i=0; i<fieldsToCheck.length; i++) {
	var f = this.getField(fieldsToCheck[i]);
	if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);
}

if (emptyFields.length>0) {
	app.alert("The following fields are required:\n" + emptyFields.join("\n"),1,0, 'Validation Error');
} else app.execMenuItem("SaveAs");

1 reply

Bernd Alheit
Community Expert
Community Expert
April 6, 2022

Check the page property of the fields.

Participating Frequently
April 6, 2022

Sorry, I am really new to creating forms in Adobe. What do you mean exactly?

Bernd Alheit
Community Expert
Community Expert
April 6, 2022

In your script you checks the properties type, required, and so on. Check also the page property.