Skip to main content
New Participant
June 1, 2013
Answered

Check to see if any field is empty

  • June 1, 2013
  • 18 replies
  • 85386 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

Participating Frequently
November 10, 2016

Script works great - thank you. However if one of my fields equals zero (e.g. I have a box for carriage costs which can be zero if customer is collecting), script doesn't recognise this as completed and assumes zero means empty and so error dialog pops up.

Can you help, please?

try67
Community Expert
November 10, 2016

Change this part of the code:

(f.type=="text" && f.value=="")

To this:

(f.type=="text" && f.valueAsString==f.defaultValue)

Participating Frequently
November 10, 2016

Perfect - thanks very much!

kyleb90859659
New Participant
November 3, 2016

I am using the below to print only the page that has been filled out to send in.  Right now if it finds errors it does pop up a window that shows what needs to be filled out.  However after clicking okay it still prints the document.  What am i missing to not print if error is reported.

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

};

this. print({bUI: false, nStart: pageNum, nEnd: pageNum, bSilent: true, bShrinkToFit: true});

try67
Community Expert
November 3, 2016

Remove any print command you added, ad well as the semi-colon at the end of

the code and add this line as the last line:

else this.print();

March 7, 2016

Is there anywhere is this tread that specifies how to only highlight required fields that were not completed after using the submit form option? or does that have to be done through scripting at the field level?

28110942
New Participant
March 7, 2016

put the document level script on "Page Open":

app.runtimeHighlight = false;

Then put Try67's script with Submit:

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.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));

}

else

{

// This is the form return email. It's hardcoded

// so that the form is always returned to the same address.

var cToAddr = "email@domain.com"

// Set the subject and body text for the email message

var cSubLine = "Form Submission"

// Send the entire PDF as a file attachment on an email

this.mailDoc({bUI: true, cTo: cToAddr, cSubject: cSubLine});

}

New Participant
July 14, 2015

Hi Guys

In the below script It very useful & it's work perfectly but in the script (text fields & check fields) only it consider other than Dropdown box and List box  are not considering. Can you please help me out how to include those dropdown & list box in this script


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

}

dewe14
Participating Frequently
July 2, 2015

Hi Try67,

I have set your code into my pdf document (Will Print and Will Save Menu), when I go to File - Print or File - Save warning was appear "Error! You must fill in the following fields" that's clear. But, unfortunately my document was still printed and saved after that warning.

How to prevent my document if field not complete will stay not print or save ?

Quote :

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

}

Thank you

try67
Community Expert
July 2, 2015

You can't prevent that entirely. One possibility, though, is to have an error message show up on the form itself, saying it is incomplete.

dewe14
Participating Frequently
July 3, 2015

Hi Try,

Thanks for response, is it possible if we set disable print menu after alert show or data blank or what ever that inform user that he doesn't complete it?

My document already show alert message and set focus to that blank field, but after that document still printed.

Thank you Sir,

November 20, 2014

Hey, does anyone know how to make an app.alert if the required fields are all filled in. ie. one or more characters? Something like, "All required fields are filled in "

Inspiring
November 20, 2014

It is the "else"  if there are no uncompleted fields. It was included in my earlier post.

Inspiring
June 1, 2013

Why not just use the number of fields and the get nth field name with the getField to get and test each page as needed?

You will need to account for fields that do not have a value.

try67
Community Expert
June 1, 2013

You don't necessarily need an array with the names of the fields, but you do need some way of determining which fields to control...

So do you want to control all of the fields in the file? Or all of the required ones?

nexx222Author
New Participant
June 1, 2013

All required fields, out of the 200+ only about 10 dont need to be filled.

GKaiseril: Thats what Im doing now. I have an array:

var a1 = new Array(getField("box1"), getField("text1"));

for (number of items in a1)

   check if empty

      popup box if empty

the problem is typing in the array "box1", "text1" since I have over 200 fields to type in

try67
Community Expert
April 24, 2015

I have 3 PDFs that I want users to fill out, save and email back. I used the script above from Try67, and it works wonderfully on 2 of them. It alerts you if you try to save without filling in all of the required fields, which is exactly what I want it to do. But for some reason it's not working on the third PDF. I built all 3 in InDesign and exported as Interactive PDFs. I made sure that all of the text fields have unique names. The 3 PDFs are very similar, so I can't figure out why it's not working on one of them. Does anyone have any idea why?

I'm using Acrobat Pro XI that comes with Creative Cloud on the Mac. I'm going to Set Document Actions and then choosing "Document Will Save", and pasting in the script. 


What type of field is it? Is it set as required? Are there any error messages in the JS console?