Newbie to JavaScripting and need some guidance.. Not sure if im setting this correct but I have (2) JavaScripts on "Mouse Up" trigger in a email button. First java script runs the validation of the required fields and the second attaches the PDF in a email. I have everything working fine except that when there is a missing required field it prompts the user but still sends the email. I want the script to stop and not let the user send the email taking them back to the form till all required fields are met. Using Adobe Pro 9..
//JavaScript #1 (Validates the required fields)
var requiredFields = new Array();
for (var i = 0; i < this.numFields; i++)
{
var fName = this.getNthFieldName(i);
var f = this.getField(fName);
if (f.type!="button" && this.getField(fName).required && (this.getField(fName).value == null || this.getField(fName).value == ''))
{
requiredFields[requiredFields.length] = fName;}}
var error = "Please complete the following fields: \n\n";
for (j=0; j < requiredFields.length; j++)
{
if (requiredFields[j].value == null)
{
error = error + requiredFields[j] + '\n';
var f2 = this.getField(requiredFields[j]);
f2.setFocus();
}
}
if (requiredFields.length > 0)
app.alert(error);
//JavaScript #2 (this attaches the pdf to the email)
{
// This is the form return email, It’s hardcoded
// so that the form is always returned to the same address
// Change address on your form
var cToAddr = "Miami DDA Services HBUS";
// Set the subject line
var cSubLine = "FX DDA AND/OR CALL ACCOUNTS";
// Send the form data as an PDF attachment on an email
this.mailDoc({bUI:true, cTo: cToAddr,cSubject: cSubLine});
}
if (requiredFields.length > 0) {
app.alert(error);
} else {
// JavaScript #2 goes here
}
Also, code like this:
(this.getField(fName).value == null || this.getField(fName).value == '')
can be simplified to:
this.getField(fName).value == '')
Since a field value will never be equal to the special JavaScript value of null. It is misleading to test for it. A blank text field value will be equal to the empty string (""), so that's the value you should test for.