• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

500

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

Community Expert , 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
...

Votes

Translate

Translate
Community Expert ,
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

Votes

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.

Votes

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