Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Run additional Javascript only after required fields are filled

New Here ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

Complete noob with JavaScript. Please help.

Using following (which seems to work) to check if required fields are filled:

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.valueAsString==f.defaultValue) emptyFields.push(f.name);

}

}

if (emptyFields.length>0) {

app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));

}

 

Would like to run following (I've deleted personal info) ONLY when above indicates all required fields are filled:

var mySub=this.getField("Last Name").value+", "+
this.getField("First Name").value+" - text "+
this.getField("Date").value;

this.mailDoc({cTo:"sample@sample.ca",cSubject:mySub,cMsg:mySub});

 

But I don't know how to merge these two in the code. Any help is greatly appreciated!

 

TOPICS
JavaScript, PDF forms

Views

242

Likes

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

Adobe Community Professional , May 28, 2021 May 28, 2021
In the original post where I provided you with that code, I also showed how to include code for the case where all required fields are filled.  Basically just put an "else" on the "if" Like this: if (emptyFields.length>0) { app.alert("Please complete the following fields:\n" + emptyFields.join("\n")); }else{ var mySub=this.getField("Last Name").value+", "+ this.getField("First Name").value+" - text "+ this.getField("Date").value; this.mailDoc({cTo:"sample@sample.ca",cSubject:my...

Likes

Translate

Translate
Adobe Community Professional ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

In the original post where I provided you with that code, I also showed how to include code for the case where all required fields are filled.  Basically just put an "else" on the "if"

 

Like this:

if (emptyFields.length>0) {
   app.alert("Please complete the following fields:\n" + emptyFields.join("\n"));
}else{
   var mySub=this.getField("Last Name").value+", "+
   this.getField("First Name").value+" - text "+
   this.getField("Date").value;

   this.mailDoc({cTo:"sample@sample.ca",cSubject:mySub,cMsg:mySub});
}

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
May 28, 2021 May 28, 2021

Copy link to clipboard

Copied

LATEST

Thanks, Thom. I'll try that. I hadn't seen your original post.

Likes

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