0

By reading other comments on this forum, I've put together a Submit button with JavaScript in Acrobat X that will email a certain person and include information from fields in the document in the Subject line and in the Body of the email. This part of the button works great and doesn't need changing.

Here's what I'm having trouble with: I want an error window to come up if the user hasn't completed all the Required fields (I've marked about 10 of them as Required), and I want this error to prevent that aforementioned email window from popping up. This actually works (with an IF/Else statement) in the code below, but the problem is that I don't want the error if a user enters "0" as a value. I want "0" to be seen as a real, non-empty field. As it is now, if a user enters "0" in any of the Required fields, it gives them the "Oops!" error, even though "0" could be a perfectly valid answer in this case.

I think the problem is with my "if (emptyFields.length>0)" statement or something in there, but I don't know how to correct it.

I'm sure this is an extremely obvious solution, but as a total JavaScript novice, it's eluding me. I've spent the last several hours looking at various comments, but I'm still not familiar enough with the JavaScript language to apply the information in those comments... Any help is appreciated.

 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("Oops! Please fill in the following field(s) before emailing:\n\n" + emptyFields.join("\n"));

 } else {

 var cToAddr = "emailaddress@company.com"

 var cSubLine = "TD Submitted - " + this.getField("Store").value + ", SKU " + this.getField("SKU").value;

 var cBody = "Hello,\n\n" + "Completed TDR attached."
 + "Thanks!\n\n"
 + this.getField("Name").value

 this.mailDoc({
 bUI: true,
 cTo: cToAddr,
 cSubject: cSubLine,
 cMsg: cBody});
 }

Thanks.

Jacob
  • 3
  • 2

1 Answers1

1

When you compare to "", javascript may cast the empty string to a number to compare. So when you check 0 == "", javascript converts the "" to a 0 and sees that they are equal. You can use === to avoid that conversion.

0 == ""; // true
0 === ""; // false

So your statement might be replaced with:

(f.type=="text" && f.value=="" && f.value !== 0)

This answer is much more detailed: https://stackoverflow.com/a/359509/6083834

Community
  • 1
  • 1