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:DatesWhen tripping a form field trigger:
ReferenceError: Dates is not defined
1:Field:Mouse DownCode:
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?