I am very new to using Javascript. I am using Acrobat 8 Pro. I am trying to create an e-mail button on a form, but want to check that required fields have been completed before sending the email. I have the following code to verify that required fields have been completed, and if not complete ... prompts the user to the field or fields needing attention. My problem is adding the attach to email function - I can't seem to get it right (I've eliminated my attach to email code from the following as it was creating 8 emails.) Can anyone assist? Thank you in advance.
// These are the required fields on the form.
// Populate array with their names.
var requiredFields = new Array(;
requiredFields[0] = "Traveler";
requiredFields[1] = "Email";
requiredFields[2] = "City";
requiredFields[3] = "State";
requiredFields[4] = "TravelBegin";
requiredFields[5] = "DeptName";
requiredFields[6] = "Account";
requiredFields[7] = "Balance";
var alertMsg = new Array(;
alertMsg[0] = "Please enter the name of the traveler.";
alertMsg[1] = "Please enter a valid city e-mail address so that if the request is denied ... it will be returned to the proper person.";
alertMsg[2] = "Please enter the name of the city you are traveling to.";
alertMsg[3] = "Please enter the name of the state you are traveling to.";
alertMsg[4] = "Please enter the first date of travel.";
alertMsg[5] = "Please enter the department name for the traveler.";
alertMsg[6] = "Please enter an account number to charge expenses to - one account only.";
alertMsg[7] = "Please enter the current balance remaining in the account you intend to charge the travel to.";
var emptyTest = /^\s*$/;
var fieldCount = requiredFields.length
var fld = 0;
for (var i=0; i < fieldCount; i++) { fld = this.getField(requiredFields[i]); if( emptyTest.test(fld.value) ) // if required field is empty { app.alert(alertMsg[i]); fld.setFocus(); break; }
}
What you're missing is a success/failure variable that is set if an empty field is found.
You should place the email function in an "if" statement, After The Loop (not inside of it), that uses this variable to either send, or not send, the email.
Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script