Skip to main content
September 29, 2016
Answered

How do I check if a field is empty? The code I have thinks a "0" is empty, which is false.

  • September 29, 2016
  • 3 replies
  • 1888 views

The code I used is copied from this thread: https://forums.adobe.com/thread/1223607

I created a button that will list out each field if it is left blank. However, a field will still pop up even though there is a character in it, namely "0".

Please help.

This topic has been closed for replies.
Correct answer George_Johnson

I expected you would change the code based on what I showed. So change that one line to:

          if ((f.type == "text" && f.valueAsString === "") || (f.type == "checkbox" && f.value === "Off")) emptyFields.push(f.name);

3 replies

associationa93135951
Participant
October 23, 2016

Tks !

Actually if all the mandatory fields are not completed, the script stops. I don't want it to stop until all the fields are completed because when all fields are completed I have a second command allowing to print and save.

All is in form created with acrobat XI.

I don't know how to send you if necessary

associationa93135951
Participant
October 23, 2016

Hello,

Maybe it's not the correct place, but I use the same script but have a roblem : I want tol 'loop' until all the fields are completed /

What's wrong in my current script ?

Thanks

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("Attention !\Vous n'avez pas complété vos références suivantes\n\n" + emptyFields.join("\n"));                   Si la condition n’est pas réunie il faut recommencer jusqu’à ce que tout les champs soient remplis :

}

if (emptyFields.length==0) {

app.alert("Vos références sont remplies, vous pouvez imprimer",3); La j’appelle une autre fonction qui marche

}

try67
Community Expert
Community Expert
October 23, 2016

What do you mean by "loop until all the fields are completed"? Please clarify.

Inspiring
September 30, 2016

You have to get the value as a string using the valueAsString field property and compare it to a zero length string (""). For exmaple:

var sVal = getField("Text1").valueAsString;

if (sVal === "") {

    app.alert("The field is empty");

}

This is equivalent:

if (!sVal) {

    app.alert("The field is empty");

}

or similarly:

if (sVal) {

    app.alert("The field is not empty");

}

September 30, 2016

I'm afraid the code won't work for me as it only tests 1 field. The code I'm using now tests all the fields in the PDF:

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!\nYou must fill in the following fields:\n\n" + emptyFields.join("\n"));

}

if (emptyFields.length==0) {

     app.alert("All fields completed. Thank you!",3);

}

George_JohnsonCorrect answer
Inspiring
September 30, 2016

I expected you would change the code based on what I showed. So change that one line to:

          if ((f.type == "text" && f.valueAsString === "") || (f.type == "checkbox" && f.value === "Off")) emptyFields.push(f.name);