Copy link to clipboard
Copied
I have a pretty large document that has over 200 text/check boxes. What I want to do is create a button that makes a pop-up warning for any blank fields in the entire document.
Right now the only way I've seen to do this is using javascript using an array of every field and checking them 1 by 1 in a for-loop. I would like to avoid this as the array requires you to type the title of each field so with 200+ fields it becomes quite tedious typing them all in.
Using Acrobat XI Pro, thanks.
You can use something like this:
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"));
}
Copy link to clipboard
Copied
Thank you so much try67! You're a genius truly.
It has works!
var bSuccess = true;
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.valueAsString==f.defaultValue) emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
bSuccess = false;
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}if(bSuccess)
mailDoc({ bUI: false, cTo:"name@.com;name@.com", cCc:"name@.com", cSubject: "Write the title of the recharge here", cMsg: "Please write the title of your recharge and attach all relevant invoices together with signed correctness and approval and email approval for the recharge here." });
Copy link to clipboard
Copied
A couple of questions on this. Let me know if I should be posting new threads for each.
Here is my full code in case that is helpful:
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.display==display.visible) {
if ((f.type=="text" && f.value==""&& f.value !== 0) || (f.type=="checkbox" && f.value=="Off") || (f.type=="dropdown" && f.value==""))
f.strokeColor = color.red;
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("You must complete the following fields:\n" + emptyFields.join("\n"));
}
Thanks in advance!
Copy link to clipboard
Copied
Replace this line:
if ((f.type=="text" && f.value==""&& f.value !== 0) || (f.type=="checkbox" && f.value=="Off") || (f.type=="dropdown" && f.value==""))
With:
if (f.valueAsString==f.defaultValue)
Copy link to clipboard
Copied
Thank you, try67.
While those fields show in my pop-up as needing to be completed, they are not then highlighted in red. Can I fix that?
And, how do I reset my form to remove the red highlights as I continue to work on my form?
Copy link to clipboard
Copied
Please post your full code.
Copy link to clipboard
Copied
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.display==display.visible) {
if (f.valueAsString==f.defaultValue)
f.strokeColor = color.red;
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("YOU MUST COMPLETE THE FOLLOWING FIELDS:\n" + emptyFields.join("\n"));
}
Copy link to clipboard
Copied
You're missing a pair of curly brackets, after the inner-most if-condition. Use this:
var emptyFields = [];
for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.display==display.visible) {
if (f.valueAsString==f.defaultValue) {
f.strokeColor = color.red;
emptyFields.push(f.name);
} else {
f.strokeColor = color.transparent;
}
}
}
if (emptyFields.length>0) {
app.alert("YOU MUST COMPLETE THE FOLLOWING FIELDS:\n" + emptyFields.join("\n"));
}
Copy link to clipboard
Copied
You are a lifesaver!
I hate to beat a dead horse but: how do I reset the red highlights?
Copy link to clipboard
Copied
Forgot to mention, but I added that too to the code above...
Copy link to clipboard
Copied
Thank you but I don't think I explained myself. Since I am still working on the document and testing things left and right, I would like a way of removing the red highlights and starting fresh. Like before I ran the script. Also, once I finalize the document, I don't want it to be sent out with a bunch of red highlighted fields.
I hope that makes sense.
Copy link to clipboard
Copied
Then just execute the part of the script that removes the red border, ie:
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.display==display.visible) {
f.strokeColor = color.transparent;
}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
My bad on the caps. I was in the middle of data entries in my spreadsheets.
Copy link to clipboard
Copied
Hi,
Tried to copy this code but can't seem to make it work for me. I have a PDF form that needs to be completed and reviewed by 3 departments, each section is signed and dated. I have required fields that are text and the other is date. I added this code into a button to validate prior to signing. I also want to hide the signature option until required fields are completed. If you could please help me with this I would be really really grateful.
thanks in advance!
Copy link to clipboard
Copied
try67:
Your code (listed below for convenience) doesn't account for if the button including this javascript is also required. How can you modify this code for a checkbox button that's used to execute the javascript to be left "unchecked" or "off" if any required fields are blank/off?
This was the code you had previously posted:
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"))
f.strokeColor = color.red; //Highlights the required fields in red
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
}
Copy link to clipboard
Copied
I've been searching for this script. THANK YOU GILIAD.
Copy link to clipboard
Copied
Hey,
I m using the code with the emptyfield but I would like to apply it for only specific pages without using textfield names . Is it possible to extract the page number and add a "if" to the code?
Copy link to clipboard
Copied
Yes, post this to a new thread.
Copy link to clipboard
Copied
Hi,
Now the pop-up message shows the “Name” field, could you help to add the content in the field name “Tooltip” in the pop-up message, please. We would like to show the content in the tooltip field using Cyrilic letters.
Tahnk you in advance.
Copy link to clipboard
Copied
In the code above replace this line:
emptyFields.push(f.name);
With:
emptyFields.push(f.userName);
Copy link to clipboard
Copied
Hi try67, I have used your script below and it works perfectly, I am however looking to see if it is possible to return the page number that the empty fields are on, is that possible?
Copy link to clipboard
Copied
Here is what I have but it only gives the page number of where the button is
var emptyFields = [];
var PageString = pageNum;
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") + " Page - " + PageString);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Remove what you added and change this:
emptyFields.push(f.name);
To:
if (typeof f.page=="number")
emptyFields.push(f.name + " (p." + (f.page+1) + ")" );
else emptyFields.push(f.name + " (p." + (f.page[0]+1) + ")" );
Copy link to clipboard
Copied
This line:
var PageString = pageNum;
gives you the number of the current page.