Skip to main content
Participant
February 21, 2022
Answered

JavaScript - Functions not running or giving error

  • February 21, 2022
  • 2 replies
  • 2118 views

Hi all!

I have attached the Form in question; my JS code is throwing some errors and I cannot figure out what the problem is.

Errors:

When opening the document:

SyntaxError: missing ; before statement
2:Document-Level:Dates

When tripping a form field trigger:

ReferenceError: Dates is not defined
1:Field:Mouse Down

Code:

The following is a copy paste from Document's All JS from Foxit PDF Editor Pro (this is where I created the code and where is runs successfully):

//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------

//<Document-Level>
//<ACRO_source>Dates</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:Document-Level:Dates ***********-->
function Dates(field){
let today = new Date();
let date = `${today.getFullYear()}-${today.getMonth()+1}-${today.getDate()}`;
return this.getField(field).value = date;
}
//</ACRO_script>
//</Document-Level>

//<Document-Level>
//<ACRO_source>Required</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:Document-Level:Required ***********-->
function Required()
{

let c = 0;

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

    let fieldName = this.getNthFieldName(i);

    let f = this.getField(fieldName);

    if (f.type != "button") {

      if (f.required == true && (f.value == "" || f.text == "")) {

        c++;

      }

    }
  
}

  if (c > 0) {

    app.alert("At least one required field is empty...",1);
  c = 0;

  }

return;

}
//</ACRO_script>
//</Document-Level>

//<AcroForm>
//<ACRO_source>page1:Date Prepared:Annot3:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Date Prepared:Annot3:MouseDown:Action1 ***********-->
Dates("Date Prepared");
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Prepared by:Annot5:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Prepared by:Annot5:MouseDown:Action1 ***********-->
Required();
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Principal Investigator:Annot82:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Principal Investigator:Annot82:MouseDown:Action1 ***********-->
Required();
//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>page1:Date Ordered:Annot85:MouseDown:Action1</ACRO_source>
//<ACRO_script>
//<!--*********** belongs to:AcroForm:page1:Date Ordered:Annot85:MouseDown:Action1 ***********-->
Dates("Date Ordered");
//</ACRO_script>
//</AcroForm>

As you can see, I have two fields simply calling the Document function "Dates(field)" and two more fields that are calling the "Required()" function.

 

Can anyone see or understand why I am getting this error?

This topic has been closed for replies.
Correct answer Test Screen Name

Your script uses clever ECMAscript 6 code tricks including let and interpolated expressions in back tick string literals. But Acrobat doesn't use such a recent language base. Stick to the traditional basics. 

2 replies

JR Boulay
Community Expert
Community Expert
February 21, 2022

Date: I cannot understand your script, I changed it for a simpliest one.

Required: I replaced all "let" by "var".

 

Now it works fine.

 

Acrobate du PDF, InDesigner et Photoshopographe
Test Screen NameCorrect answer
Legend
February 21, 2022

Your script uses clever ECMAscript 6 code tricks including let and interpolated expressions in back tick string literals. But Acrobat doesn't use such a recent language base. Stick to the traditional basics. 

Participant
February 21, 2022

P.S. I forgot to explain what my code is doing.

Dates()

is simply getting today's date, putting it into a certain format, and returning that value to the field specified when the function was called.

Required()

is looping through each form field, making sure it is not a button, checking whether the field is required, checking if it is blank and then incrementing a counter. If the counter is greater than 0, a simple alert message will appear to remind the user to fill all of the required fields.