• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

69.0K

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

Community Expert , 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"));

}

Votes

Translate

Translate
replies 151 Replies 151
New Here ,
May 13, 2018 May 13, 2018

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

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

Copy link to clipboard

Copied

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!

Votes

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 Expert ,
Jun 07, 2018 Jun 07, 2018

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)

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

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?

Votes

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 Expert ,
Jun 07, 2018 Jun 07, 2018

Copy link to clipboard

Copied

Please post your full code.

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

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

}

Votes

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 Expert ,
Jun 07, 2018 Jun 07, 2018

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

}

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

Copy link to clipboard

Copied

You are a lifesaver!

I hate to beat a dead horse but: how do I reset the red highlights?

Votes

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 Expert ,
Jun 07, 2018 Jun 07, 2018

Copy link to clipboard

Copied

Forgot to mention, but I added that too to the code above...

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

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.

Votes

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 Expert ,
Jun 07, 2018 Jun 07, 2018

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; 

    }

}

Votes

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
Explorer ,
Jun 07, 2018 Jun 07, 2018

Copy link to clipboard

Copied

Votes

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 ,
Jul 21, 2018 Jul 21, 2018

Copy link to clipboard

Copied

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

Votes

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 ,
Aug 18, 2018 Aug 18, 2018

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!

Votes

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 11, 2020 Mar 11, 2020

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

Votes

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 ,
May 06, 2020 May 06, 2020

Copy link to clipboard

Copied

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

Votes

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 ,
May 18, 2020 May 18, 2020

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?

Votes

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 Expert ,
May 19, 2020 May 19, 2020

Copy link to clipboard

Copied

Yes, post this to a new thread.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
Sep 21, 2020 Sep 21, 2020

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.

Votes

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 Expert ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

In the code above replace this line:

emptyFields.push(f.name);

With:

emptyFields.push(f.userName);

Votes

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 ,
Oct 05, 2021 Oct 05, 2021

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?

 

Votes

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 ,
Oct 05, 2021 Oct 05, 2021

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

}

Votes

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 ,
Oct 05, 2021 Oct 05, 2021

Copy link to clipboard

Copied

Derrick5E63_0-1633432655203.png

 

Votes

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 Expert ,
Oct 06, 2021 Oct 06, 2021

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

Votes

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 Expert ,
Oct 05, 2021 Oct 05, 2021

Copy link to clipboard

Copied

This line:

  var PageString = pageNum;

gives you the number of the current page.

Votes

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