Stop Script after condition
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
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.
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 "}"
Copy link to clipboard
Copied
function mySubmitDoc() {
// rest of the code goes here
}
mySubmitDoc();
Copy link to clipboard
Copied
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.
Use the Acrobat JavaScript Reference early and often

