These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Check Required Fields before Emailing Document

SQ972
Registered: Apr 7 2008
Posts: 8

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; }

}

My Product Information:
Acrobat Pro 8.1, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The code looks fine. It walks thorugh all the fields and breaks if there is an empty one.

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

SQ972
Registered: Apr 7 2008
Posts: 8
How do I go about setting the success/failure variable?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Create a varialbe at the beginning of your validation script called "bSuccess" and set it to "true". If one of the tests fails, set it to false.

var bSuccess = true;

....

for (var i=0; i < fieldCount; i++)
{
fld = this.getField(requiredFields[i]);
if( emptyTest.test(fld.value) ) // if required field is empty
{
bSuccess = false;
app.alert(alertMsg[i]);
fld.setFocus();
break;
}if(bSuccess)
... do email ...

}

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

SQ972
Registered: Apr 7 2008
Posts: 8
Thank you again Thom. I have the following javascript, but nothing happens if all required fields are completed (i.e. it doesn't generate an email) and now the check for required fields does not work.

// These are the required fields on the form.
// Populate array with their names.
var RequiredFields = new Array(8);
RequiredFields[0] = "Traveler";
RequiredFields[1] = "Email";
RequiredFields[2] = "City";
RequiredFields[3] = "State";
RequiredFields[4] = "TravelBegin";
RequiredFields[5] = "DeptName";
RequiredFields[6] = "Account";
RequiredFields[7] = "Balance";



// These are the alert messages shown when a required field is empty.
// Populate array with messages.
// Make sure there's one message for each required field.
var alertMsg = new Array(8);
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 bSuccess=True
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
{
bSuccess=false;
app.alert(alertMsg[i]);
fld.setFocus();
break;
}
if(bSuccess)
this.mailDoc(true);
}


Any thoughts?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Opps, I put the "if" statement inside the loop. It needs to be below, and outside, the loop.

However, it should have sent an email anyway. In fact it should have sent one everytime around the loop.

run the mail function inside your console window and see what happens. If you haven't used the console window, then you need to learn about it. It's mandatory for code developement.

http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/javascript_console/

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

SQ972
Registered: Apr 7 2008
Posts: 8
Thank you Thom for all of your assistance! I looked at the statement first thing this morning and realized the success statement was caught in the loop. Thank you too for the info on the console, I've learned a lot from all of your articles:)
Jaxon
Registered: Oct 8 2008
Posts: 1
Any chance you could post the full javascript? I'm having trouble getting results. Thanks
Runolfr
Registered: Oct 1 2008
Posts: 119
I had some success by putting the following in my preSubmit event...

// Validate before continuing
if (FormName.execValidate() == true)
{
// Code that completes submission
}
maxwyss
Registered: Jul 25 2006
Posts: 255
In order to improve the "user experience", it might be useful to create a "summarized" alert message, instead of an individual message for each field validated. The user will then get a list of fields which need attention at once, and would therefore be faster to accomplish the work with the form.

In such a scenario, you would define another variable, and then add your error strings to it (probalby separated with the newLine character). If either your error message string has a certain length, or your "incomplete" flag gets set, you will then display the assembled message in the alert box.

And if you want to be even fancier, you could actually change a visible property of the field (strokeColor, fillColor, lineWidth, etc.) to provide a visual hint to the user, which fields are missing. ...simply don't forget to reset that property when you validate again...

Hope this can help.

Max Wyss.
R_Boyd
Registered: Nov 1 2007
Posts: 151
This is the sort of thing we're considering we are just stuck outputting the contents of the variable into the form (presumably via a text field / item). Any suggestions.

this.rawValue = errorString1.rawValue;
R_Boyd
Registered: Nov 1 2007
Posts: 151
Here's a further sample of the script we've drafted

if (form1.flowSubForm.Page1.part1FlowContainer.questionContainer.recruitMan.nameRecruitMan.rawValue != null
|| form1.flowSubForm.Page1.part1FlowContainer.questionContainer.recruitMan.nameRecruitMan.rawValue = “”)
{
errorMessage = errorMessage + “\n \t-Name of Recruiting Manager”;
form1.flowSubForm.Page1.part1FlowContainer.questionContainer.recruitMan.nameRecruitMan = “255,0,0”;
}


On a separate matter we haven't been able to find a way to turn the border of the ui part of text fields to turn red. Most examples highlight the whole field instead.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Instead of using a text box on the form. What about a popup alert.

app.app.alert(errorMessage);

Setting the border color of a field in an LC document can be tricky. Acrobat doesn't always update the display. for example your code above should be:

form1.flowSubForm.Page1.part1FlowContainer.questionContainer.recruitMan.nameRecruitMan.borderColor = "255,0,0";

if this doesn't cause a color change, then add this line

xfa.layout.relayout();

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

brzap
Registered: Jan 27 2010
Posts: 6
I am new to this forum and have found some very useful information. I'm wondering if someone can help me out. I have a fillable form that the user will complete and submit via email as a pdf. When the user clicks the button to email the pdf I want 2 things to happen: 1. check to make sure that all of the required fields are completed and 2. lock all of the fields so that the form cannot be altered by the recipient.

I have found script that allows me to "protect" all of the fields when the "submit" button is clicked however, if a required field is incomplete the user cannot complete the field because the form is locked.

Thank you in advance to anyone that is willing to give me some guidance.

brzap
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
brzap, please post this to a new thread and include the forms technology you are using.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script