Skip to main content
xavierx88691141
Participant
Aug 8, 2018 2:29 PM UTC
Answered

Empty fields notification script on a PDF form

Hi everyone,

I have built a PDF form and I need to put a control script on the Print button so that it checks empty fields right before printing the form.

Pretty easy right? Not exactly...

I have found this nice post from user try67​ who proposed a very cool script Re: Check to see if any field is empty 

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

}

The thing is I need to run the control only on three specific fields of the form, not all of them. I have no idea on how to put this as a script.

Could somebody help me with that?

Thank you in advance.

Xavier

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Aug 8, 2018 2:39 PM UTC

Use this code (adjust the field names in the first line, of course):

var fields = ["Field1", "Field2", "Field3"];

var emptyFields = [];

for (var i=0; i<fields.length; i++) {

    var f = this.getField(fields);

    if (f.valueAsString==f.defaultValue) {

        emptyFields.push(f.name);

    }

}

if (emptyFields.length>0) {

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

} else this.print();

4 replies

teresacsrocha
Participating Frequently
Apr 28, 2021 3:52 AM UTC

hello everyone. I have created a pdf form, and i needed a button for the user to attach files to it (it is an application form). Got the script for it but now i have another problem: i need those fields to be required. In short: i need a button tthat attaches the person's file ( like her/his resume) and also be a required field, meaning, if there is no attachment, the form can not be sent. Anyone with an idea to help me? This is what I have been trying for the last four hours and got nowhere: 

1- tried to create a text button that can be set as required, but even though the script works, form doesnt understand that the button doesnt need to be filled with text.

2-tried to create a checkbox and link it with the field so when file is attached, checkbox is filled - no success... maybe this could be a way but i have no idea how to execute it

3- tried to use the plain button and add a script for making it required but couldnt find a script that works...

So please!! help me

 

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Aug 8, 2018 2:39 PM UTC

Use this code (adjust the field names in the first line, of course):

var fields = ["Field1", "Field2", "Field3"];

var emptyFields = [];

for (var i=0; i<fields.length; i++) {

    var f = this.getField(fields);

    if (f.valueAsString==f.defaultValue) {

        emptyFields.push(f.name);

    }

}

if (emptyFields.length>0) {

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

} else this.print();

Translate
xavierx88691141
Participant
Aug 8, 2018 3:59 PM UTC

Works perfectly! Thanks mate!

Translate