Copy link to clipboard
Copied
Hi everyone,
I have built a PDF form and I need to put a control script on the Print button so that it checks empty fields right before printing the form.
Pretty easy right? Not exactly...
I have found this nice post from user try67 who proposed a very cool script Re: Check to see if any field is empty
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("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}
The thing is I need to run the control only on three specific fields of the form, not all of them. I have no idea on how to put this as a script.
Could somebody help me with that?
Thank you in advance.
Xavier
Use this code (adjust the field names in the first line, of course):
var fields = ["Field1", "Field2", "Field3"];
var emptyFields = [];
for (var i=0; i<fields.length; i++) {
var f = this.getField(fields);
if (f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
} else this.print();
Copy link to clipboard
Copied
Use this code (adjust the field names in the first line, of course):
var fields = ["Field1", "Field2", "Field3"];
var emptyFields = [];
for (var i=0; i<fields.length; i++) {
var f = this.getField(fields);
if (f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
} else this.print();
Copy link to clipboard
Copied
Works perfectly! Thanks mate!
Copy link to clipboard
Copied
In doing this, what if instead you wanted something more descriptive. Field 1 being maybe fName, 2 is lName, 3 being address1. Rather than putting those field names, I would like to do something like You must fill out the following fields: First Name, Last Name, Address Line 1, etc. Can anyone advise modification of the above to do that?
Copy link to clipboard
Copied
hello everyone. I have created a pdf form, and i needed a button for the user to attach files to it (it is an application form). Got the script for it but now i have another problem: i need those fields to be required. In short: i need a button tthat attaches the person's file ( like her/his resume) and also be a required field, meaning, if there is no attachment, the form can not be sent. Anyone with an idea to help me? This is what I have been trying for the last four hours and got nowhere:
1- tried to create a text button that can be set as required, but even though the script works, form doesnt understand that the button doesnt need to be filled with text.
2-tried to create a checkbox and link it with the field so when file is attached, checkbox is filled - no success... maybe this could be a way but i have no idea how to execute it
3- tried to use the plain button and add a script for making it required but couldnt find a script that works...
So please!! help me