Page Validation

New Here ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

TOPICS
How to , JavaScript

Views

160

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

correct answers 1 Correct answer

Community Expert , Apr 06, 2022 Apr 06, 2022

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

Likes

Translate

Translate
Community Expert ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

Check the page property of the fields.

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

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
Community Expert ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

Ah i see what you are saying now. Thanks. Where do I insert that at and how? I tried to, but it it was still checking the entire document.

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
Community Expert ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

That's not so easy to do, as the page property can be either a number (if there's only one field with that name) or an array, with each value in it representing the page of one of the fields in the group.

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

Ok thanks. What is the best way to build an array of the required fields on that page and have the app alert join the field names together like the below if they are blank?

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

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
Community Expert ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

The code does that for you... Post it here, if it doesn't, and I'll help you adjust it.

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

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
Community Expert ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

LATEST

Works perfectly! Thank you!!!!

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

Make content for your business needs with Adobe Express.

Get started easily with free templates: