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");
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");
Copy link to clipboard
Copied
Check the page property of the fields.
Copy link to clipboard
Copied
Sorry, I am really new to creating forms in Adobe. What do you mean exactly?
Copy link to clipboard
Copied
In your script you checks the properties type, required, and so on. Check also the page property.
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.
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.
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');
}
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.
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");
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");
Copy link to clipboard
Copied
Works perfectly! Thank you!!!!