Skip to main content
Participant
Jun 1, 2013 7:15 AM UTC
Answered

Check to see if any field is empty

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jun 1, 2013 9:16 PM UTC

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

}

151 replies

Participating Frequently
Nov 10, 2016 4:13 PM UTC

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?

Translate
try67
Community Expert
Community Expert
Nov 10, 2016 4:16 PM UTC

Change this part of the code:

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

To this:

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

Translate
Participating Frequently
Nov 10, 2016 4:25 PM UTC

Perfect - thanks very much!

Translate
kyleb90859659
Participant
Nov 3, 2016 12:34 PM UTC

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

Translate
try67
Community Expert
Community Expert
Nov 3, 2016 1:30 PM UTC

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

Translate
Mar 7, 2016 10:32 PM UTC

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?

Translate
28110942
Participant
Mar 7, 2016 11:42 PM UTC

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

}

Translate
Participant
Jul 14, 2015 5:46 AM UTC

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

}

Translate
dewe14
Participating Frequently
Jul 2, 2015 3:25 AM UTC

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

Translate
try67
Community Expert
Community Expert
Jul 2, 2015 6:41 AM UTC

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.

Translate
dewe14
Participating Frequently
Jul 3, 2015 3:24 AM UTC

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,

Translate
Nov 20, 2014 4:34 AM UTC

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 "

Translate
Inspiring
Nov 20, 2014 6:23 PM UTC

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

Translate
Inspiring
Jun 1, 2013 2:57 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Jun 1, 2013 9:37 AM UTC

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?

Translate
nexx222Author
Participant
Jun 1, 2013 8:27 PM UTC

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

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jun 1, 2013 9:16 PM UTC

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

}

Translate