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

Stop Script after condition

Community Beginner ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

Hi

I have the following sctipt that run on a submit button.

 

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

var fname = this.getNthFieldName(i);
var f = this.getField(fname);

if (f.type != "button" &&  f.required == true) {

if (f.value == "") {

app.alert({

cMsg: "Please complete required field(s) before submitting this form: " + f.name,

cTitle: "Required Fields Check "});

}
}}
   

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

var fname = this.getNthFieldName(i);
var f = this.getField(fname);

if (f.type != "button" &&  f.required == true) {

if (f.value == "") {

app.alert({

cMsg: "Please complete required field(s) before submitting this form: " + f.name,

cTitle: "Required Fields Check "});

}
}}
   


var cToAddr = "Testemail@Email.com"

var cCCAddr = "lcferreira@remsens.co.za";


var cSubLine = "Form X-1 returned from client"
var cBody = "Thank you for submitting your form.\n" + "Save the filled form attachment for your own records"

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

var cToAddr = "Testemail@Email.com"

var cCCAddr = "lcferreira@remsens.co.za";


var cSubLine = "Form X-1 returned from client"
var cBody = "Thank you for submitting your form.\n" + "Save the filled form attachment for your own records"

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

 I want to stop the email from sending if all fields are not completed. If gives me the error but then the rest of the script runs and the email is generated.

 

I tried return, but it gives an error. Please help 

TOPICS
JavaScript , PDF forms

Views

1.4K

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
Community Expert ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

There are a couple of ways of doing it.

One is to put the code in a function (and calling it, of course) and then calling return will work as it will immediately exit the function.

Two is to use boolean a variable to check if any fields were empty. If so, display the error message. If not, mail the document.

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
Community Beginner ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

Can you please give me an example of where it goes in. I added it and gives me a "return is outside of funtion"no matter what I do with the "}"

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
Community Expert ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

function mySubmitDoc() {

// rest of the code goes here

}

 

mySubmitDoc();

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
Community Expert ,
May 27, 2021 May 27, 2021

Copy link to clipboard

Copied

LATEST

Here's a rewrite of the script that collects a list of unfilled, required fields. This is used both for the message, so it's only displayed once, and for blocking the submit if this list is not empty. 

 

 


var aIncompleteFlds = [];

for ( var i=0; i < this.numFields; i++) {
   var fname = this.getNthFieldName(i);
   var f = this.getField(fname);
   if ((f.type != "button") &&  (f.required == true) && (f.value == f.defaultValue)) {
       aIncompleteFlds.push(f.name);
}

if(aIncompleteFlds.length > 0)
    app.alert({cMsg: "Please complete required field(s) before submitting this form: " + 
               aIncompleteFlds.join(","),  cTitle: "Required Fields Check "});
else
{
    var cToAddr = "Testemail@Email.com"
    var cCCAddr = "lcferreira@remsens.co.za";
    var cSubLine = "Form X-1 returned from client"
    var cBody = "Thank you for submitting your form.\n" + "Save the filled form attachment for your own records"
    this.mailDoc({bUI: true, cTo: cToAddr, cCc: cCCAddr, cSubject: cSubLine, cMsg: cBody});
}

 

 

Notice also that the fields defaultValue property is used to determine whether or not the field is filled. 

 

 

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