Need a few javascript actions to occur when the submit button is clicked

New Here ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

Still working on the same form from last week! Here is my challenge at the moment. The client wants to be able to have the user choose an email address to send the form to from a drop-down, prior to submit. (issue solved) Then on submit, the client wants almost every field of the form to be required and for the form to not submit until the user completes the fields. (this is my challenge) Then, if the form is properly filled out, they want the form to flatten before sending. This created issues for me because it was flattening the email address drop-down and the submit button. I figured out how to exclude those fields from the flattening, but I can't figure out how to check the required fields and stop the submit from happening. I've placed an on-blur alert on the fields but it is so annoying, and doesn't prevent the form from sending, there has to be a better way. Below is the script on the button and the script on the individual fields and I've attached the test form. Any insight or help is greatly appreciated. I'm very new to javascript and especially custom scripts in forms. Thank you.

 

var sendForm = app.alert("This form is preparing to send and will be locked.\n\n" + "Have you filled in all the required fields (red outlined fields)?\n\n",2,2,"Send Validation");

if(sendForm == 4){

var fieldsToLock = ["Legal Name", "Document Name", "Document Owner", "Description", "Reviewed", "Effective Date", "Expiration Date", "Call for Action Date", "Counter Signer", "Counter Signer Email", "Requested Date", "Recipient1", "Recipient2", "Submitted by", "Date",];
for (var i in fieldsToLock) this.getField(fieldsToLock[i]).readonly = true;

 

var cToAddr = this.getField("EmailTo").value;

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, cSubject: cSubLine, cMsg: cBody});

}
else
app.alert("Please fill in all required form fields");

 

 

f = getField(event.target.name)
if (f.value.length == 0)
{
f.setFocus()
//Optional Message - Comment out the next line to remove
app.alert("This field is required. Please enter a value.")
}

 

 

TOPICS
How to, JavaScript, PDF forms

Views

83

Likes

translate

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
Most Valuable Participant ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

So basically what you want is to verify that all the required fields are filled in before emailing the file?

If so, search the forum for "validate required fields", as I've provided a function that does just that.

The rest seem to be implemented in your code already. And by the way, since you're not really flattening the fields but just setting them as read-only it should be a problem to read the value of the email field drop-down, since a script can access the value of a field regardless of whether it's editable (or even visible).

Likes

translate

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 ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

You can try this:

 

var sendForm = app.alert("This form is preparing to send and will be locked.\n\n" + "Have you filled in all the required fields (red outlined fields)?\n\n",2,2,"Send Validation");

if(sendForm == 4){

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.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
} else {

var fieldsToLock = ["Legal Name", "Document Name", "Document Owner", "Description", "Reviewed", "Effective Date", "Expiration Date", "Call for Action Date", "Counter Signer", "Counter Signer Email", "Requested Date", "Recipient1", "Recipient2", "Submitted by", "Date",];
for (var i in fieldsToLock) this.getField(fieldsToLock[i]).readonly = true;


// Set the target email address based on the selection in the field

var cToAddr = this.getField("EmailTo").value;

// Set the subject and body text for the email message

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"

// Send the entire PDF as a file attachment on an email

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

}
}

Likes

translate

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