Skip to main content
Participant
June 1, 2013
Answered

Check to see if any field is empty

  • June 1, 2013
  • 18 replies
  • 85436 views

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.

This topic has been closed for replies.
Correct answer try67

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

}

18 replies

Inspiring
December 31, 2022

This script is perfect for me.

 

I have one question. It looks like the fields are listed in alphabetical order. It would be better for me if they were listed in tab order. Is that possible?

Thom Parker
Community Expert
Community Expert
January 2, 2023

Not possible with Acrobat JavaScript

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
October 5, 2021

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?

 

Participant
October 5, 2021

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

}

Participant
October 5, 2021

 

VD451F
Participant
September 21, 2020

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.

try67
Community Expert
Community Expert
September 21, 2020

In the code above replace this line:

emptyFields.push(f.name);

With:

emptyFields.push(f.userName);

Participant
May 18, 2020

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?

Thom Parker
Community Expert
Community Expert
May 19, 2020

Yes, post this to a new thread.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
vickib85760701
Participating Frequently
May 6, 2020

I've been searching for this script. THANK YOU GILIAD. 

Participant
March 11, 2020

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

CheDee317
Known Participant
August 18, 2018

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!

Participating Frequently
July 21, 2018

My bad on the caps. I was in the middle of data entries in my spreadsheets.

Cheena12
Inspiring
June 7, 2018

A couple of questions on this. Let me know if I should be posting new threads for each.

  • I added the f.strokeColor = color.red; command to the script and it works great. But how do I reset it to go back and continue working on my document without them?
  • I want the code to check my required dropdown boxes and highlight them as well. Here was my attempt:
    • if ((f.type=="text" && f.value==""&& f.value !== 0) || (f.type=="checkbox" && f.value=="Off") || (f.type=="dropdown" && f.value==""))
    • My dropdowns have a default option of a space since blank is not an option
    • I added a custom calculation so that a space value would be converted to no value:
      • if (event.value == " ") event.value = "";

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!

try67
Community Expert
Community Expert
June 7, 2018

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)

Cheena12
Inspiring
June 7, 2018

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?

Inspiring
November 3, 2017

try67,

This code works great for me but the button I'm using to check if all fields have been completed is a signature box.  When I click on it with missing fields, it definitely pops up what fields are missing but the signature window pops up after I click ok.  I'm trying to prevent the user from signing the document until all fields have been filled in.

try67
Community Expert
Community Expert
November 3, 2017

You can't do that. The only way is to hide the signature field and attach the code to a button field. If the validation is successful, show the signature field so the user can sign it. Otherwise, keep it hidden.

Inspiring
November 6, 2017

Darn...ok, thanks for the heads up.