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

Empty fields notification script on a PDF form

Community Beginner ,
Aug 08, 2018 Aug 08, 2018

Copy link to clipboard

Copied

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

TOPICS
PDF forms

Views

1.8K

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

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

Votes

Translate

Translate
Community Expert ,
Aug 08, 2018 Aug 08, 2018

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

Works perfectly! Thanks mate!

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 ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

LATEST

In doing this, what if instead you wanted something more descriptive.  Field 1 being maybe fName, 2 is lName, 3 being address1.  Rather than putting those field names, I would like to do something like You must fill out the following fields: First Name, Last Name, Address Line 1, etc.  Can anyone advise modification of the above to do that?

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 ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

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

 

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