Check to see if any field is empty

Community Beginner ,
Jun 01, 2013 Jun 01, 2013

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.

TOPICS
Acrobat SDK and JavaScript

Views

54.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 01, 2013 Jun 01, 2013
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"));}

Likes

Translate

Translate
replies 149 Replies 149
LEGEND ,
Oct 19, 2014 Oct 19, 2014

Copy link to clipboard

Copied

Not if you use the comparison of the field's value to the field's default value.

// required fields;
var aRequiredFields = new Array("Text1", "Text2",
"Check Box3", "Group1", "List Box5", "Dropdown6");
// messages for non-completed fields;
var aMessage = new Array();
aMessage["Text1"] = "Complete Text1";
aMessage["Text2"] = "Complete Text2";
aMessage["Check Box3"] = "Complete Check Box3";
aMessage["Group1"] = "Complete Radio Button Troup1";
aMessage["List Box5"] = "Complete List Boxt5";
aMessage["Dropdown6"] = "Complete Dropdown6";
// array  of non-compleeted field names;
var aNotCompleted = new Array();
// loop through fields and finding fields whose value si the same as their default value;
for(var i = 0; i < aRequiredFields.length; i++) {
// clear the field fill color;

this.getField(aRequiredFields).fillColor = color.transparent;

if(this.getField(aRequiredFields).value == this.getField(aRequiredFields).defaultValue) {
    // log field name into array;
aNotCompleted.push(aRequiredFields);

// set the field's fill color;

this.getField(aRequiredFields).fillColor = color.red;

} // not completed
} // end loop field names
if(aNotCompleted.length != 0) {
var cMsg = "Complete following " + aNotCompleted.length + " fields:";
for(var i = 0; i < aNotCompleted.length; i++) {
  // load field message into error message
  cMsg += "\r"+ aMessage[aNotCompleted];
  }
app.alert(cMsg, 1, 0);
} else {

// actions to complete when all required fields completed.
app.alert("Conradualtions, all fields completed", 3, 0);
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2016 Mar 02, 2016

Copy link to clipboard

Copied

Thank you all so much for posting the scripts, they're so helpful!  I put Try67's script in a Print button:

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

{

app.execMenuItem("Print");

}

It works perfectly!  Then I tried adding MichaelN's highlighting addition but it didn't work. What I'm hoping to achieve is to turn off the automatic highlighting of the required fields until the Print button is engaged, then to highlight the required fields that were not filled in.  I'm not sure if this is possible, and if it is, if I have to uncheck Required in the fields and set them as Required through JavaScript instead.  Would I need to start a new thread for this?  Thank you

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

Copy link to clipboard

Copied

I added this Document level script on Page Open:

app.runtimeHighlight = false;

Then it worked to hide the required field highlighting.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 29, 2020 Jan 29, 2020

Copy link to clipboard

Copied

I know this is a super old thread. Did you get the answer for this? I am facing the same problem too. hopefully someone can help. Below is my code

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"));
} else {
// actions to complete when all required fields completed.
 app.alert("No errors are detected, remember to save your file and get signatures.", 3, 0);
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2015 Dec 09, 2015

Copy link to clipboard

Copied

try67 wrote:

Yes. Change this line:

if (f.type!="button" && f.required ) {

To this:

if (f.type!="button" && f.required && f.display==display.visible) {

This is great, but I have buttons that show and hide certain fields. This code permanently makes the required fields visible and does not allow me to re-hide them when I click the check box that hides them. How to I fix this part? Thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 09, 2015 Dec 09, 2015

Copy link to clipboard

Copied

No, this code doesn't do that, unless you copied it incorrectly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2015 Dec 09, 2015

Copy link to clipboard

Copied

try67 wrote:

No, this code doesn't do that, unless you copied it incorrectly.

Hi Try. So when I copy paste the code, the required fields remain hidden. They do not become visible when checking the boxes.

Hidden.pngVisible.png

Code:

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("Error! You must check 'Yes' in the following boxes to qualify for Preferred Tier:\n" + emptyFields.join("\n"));

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Dec 09, 2015 Dec 09, 2015

Copy link to clipboard

Copied

Again, this script doesn't change the visibility of any fields. It just makes sure that only visible fields are validated.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2015 Dec 09, 2015

Copy link to clipboard

Copied

OK, thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 18, 2015 Feb 18, 2015

Copy link to clipboard

Copied

hello, my document appear information "You must fill filed xxxx" but after that message why my document still print ya? Pls help .

Thanks

dewe14

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

I was able to do this by also adding the javascript to a new action on a Print button.

Mouse down>run the javascript

Mouse up>Print

It would still print if you go to "File>Print" but at least it required the fields to be filled in before the print button would work.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

If you add the print command as a part of the JS code then you could abort it entirely (when the button is used) if not all the fields are filled-in.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

I realized after I posted this that it did not work. I could click okay and still save or print. Any suggestions? I probably need an "else if" statement at the end of the script.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

It all has to be done in a single script. One action can't interfere with another one.

If you post your code I could let you know how to do it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

I used this javascript

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

}

Screen Shot 2015-03-03 at 10.02.36 AM.png

This is for the "SAVE" button on a reader extended form.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

Looks familiar... 🙂

It's more tricky for a save button, but try adding this at the end (and remove the separate "Execute a menu item" command, of course):

else app.execMenuItem("SaveAs");

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

That worked! Thank you. If I want to use this for the print button, would I use ("Print")? I will try it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

Yeah, that should work.

On Tue, Mar 3, 2015 at 4:07 PM, sheilab77417713 <forums_noreply@adobe.com>

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2015 Mar 03, 2015

Copy link to clipboard

Copied

I tried it with ("Print") and it worked. Thanks try67‌ for your help.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

The following error message appears in the Javascript Debugger. The weird thing is that when I checked the Javascript Debugger in one of the files that worked, it has the same error message--but it worked for that PDF. There are text fields, some of which are required, and some are not. (I also have some check boxes and radio buttons which aren't affected by the script, but that's separate issue). The required fields are outlined in red, so I know it's recognizing them as required.      

Acrobat EScript Built-in Functions Version 11.0

Acrobat SOAP 11.0

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Field:Mouse Up

TypeError: f is null

4:Field:Mouse Up

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Field:Mouse Up

TypeError: f is null

4:Doc:Will Save

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

SyntaxError: missing ; before statement

6:

TypeError: this.getField(f) is null

4:Doc:Will Save

TypeError: this.getField(f) is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

SyntaxError: missing ) after argument list

9:

SyntaxError: missing ) after argument list

9:

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

SyntaxError: missing ; before statement

1:

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Document-Actions:Document Will Save

TypeError: f is null

4:Doc:Will Save

TypeError: f is null

4:Doc:Will Save

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

Did you put the code in the Will Save action? If so, change this line:

if (f.type!="button" && f.required ) {

To this:

if (f!=null && f.type!="button" && f.required ) {

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

It worked! You're a genius!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 13, 2015 Jul 13, 2015

Copy link to clipboard

Copied

Excellent, thank you.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines