Skip to main content
Fango24
Participating Frequently
November 27, 2017
Answered

How to fill out empty fields automatically?

  • November 27, 2017
  • 2 replies
  • 2909 views

Hi all,

I'm creating a pdf right now which has to be filled out by our customers.

Due to Audit restrictions every field has to contain a valid value. - if nothing is entered the field should display "n.a.".

My code right now:

event.rc = true;

if (event.value==""|| event.value ==""||event.value==0)

{

    event.value="n.a."

}

Problem is: It's valid only for one field and triggered by selecting the cell. - I'm searching for something like a Loop which checks all empty cells after finishing the editing.. (at saving, or signing digitally)

Many thanks in advance, I appreciate it

kindly regards Fango

This topic has been closed for replies.
Correct answer JR Boulay

Oops!

But as I wrote: not tested…

This one was tested:

for (var i=0; i<this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    if (f != null) {

        if (f.type == "text") {

            if ((f.value == "") || (f.value == " ") || (f.value == 0) || (f.value == null)) {

                f.value = "n.a.";

            }

        }

    }

}

2 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
November 27, 2017

Oops!

But as I wrote: not tested…

This one was tested:

for (var i=0; i<this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    if (f != null) {

        if (f.type == "text") {

            if ((f.value == "") || (f.value == " ") || (f.value == 0) || (f.value == null)) {

                f.value = "n.a.";

            }

        }

    }

}

Acrobate du PDF, InDesigner et Photoshopographe
Fango24
Fango24Author
Participating Frequently
November 27, 2017

That's exactly what i needed THANK you!

One small Problem still occurs:

If i enter a value within the "triggercell" i get a note, that the Format of the date is wrong (another cell within the doc), which is correct due to the fact, that i didn't insert one. - After that approx. the half of the boxes are filled with [n.a.] - If i now change the value of the triggerbox again, the error occurs another time, but now the rest gets filled out..

Do you have an idea why?

Thank you very much for your help so far ..

Thom Parker
Community Expert
Community Expert
November 27, 2017

When formatting is selected for a text field, Acrobat inserts JavaScript into the validate, format, and keystroke event scripts. It is this code that is causing the problem. Either Remove the formatting altogether, or remove the validation scripts from the date fields with this code

this.getField("DateField").setAction("Validate","");

This retains the date formatting, but removes validation.

Another option is to replace the Format Event scripts in all text fields with code to display n.a..  Then you don't need to test all the fields after  the form is filled. All fields correctly display a value all the time.

Use the same loop JR presented above to run this code

f.setAction("Format","if(/^\s*$/.test(event.value)) event.value = 'n.a.'; ");

Values set in the Format event do change the true value of the field, they only modify what is displayed in the field. So this is a great place to set the "n.a." display

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
JR Boulay
Community Expert
Community Expert
November 27, 2017

Can't you place the "n.a" as the default value?

And then use a script to automatically delete the default text when the field get the focus?

It would be easier.

Acrobate du PDF, InDesigner et Photoshopographe
Fango24
Fango24Author
Participating Frequently
November 27, 2017

Hi JR_Boulay,

I have already thought of that, but it would be much better-looking/ more professional the other way around.

Setting the default value to "n.a." is my stopgap

Thank you & regards

JR Boulay
Community Expert
Community Expert
November 27, 2017

You can try this script (not tested):

for (var i=0; i<this.numFields; i++) {

    var f = this.getField(this.getNthFieldName(i));

    if (f != null) {

        if (f.type == "text") {

            if ((f.value == "") || (f.value == " ") || (f.value == 0) || (f.value == null)) {

                event.value = "n.a.";

            }

        }

    }

}

Acrobate du PDF, InDesigner et Photoshopographe