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

Script to verify that req'd fields R complete b4 protecting & emailing

brzap
Registered: Jan 27 2010
Posts: 6

I'm wondering if someone can help me out. I am creating a fillable form using Adobe Acrobat 7.0 Professional.

I have already created all of the fields and have set the required fields to "required." The user will complete the form and submit the form as a pdf attachment via email.

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.

I need some kind of an "if" clause that IF all required fields are complete the form will be locked an emailed. Here is my script:

// Lock the form before attaching to email
for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
var oFields = xfa.layout.pageContent(nPageCount, "field");
var nNodesLength = oFields.length;
// Set the field property.
for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
oFields.item(nNodeCount).access = "protected";
}
}

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

brzap

My Product Information:
Acrobat Pro 7.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
First, given the code you posted, you're using a LiveCycle form. We need to know this stuff to answer the question.

What you need to do of course, is to test the required fields before submitting. In fact, you can use this exact same loop for the process. Just test for the "nullTest"

For example:
var rgEmpty = /^\s*$/;var bPass = true;for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {var oFields = xfa.layout.pageContent(nPageCount, "field");var nNodesLength = oFields.length;// Set the field property.for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {if ( (oFields.item(nNodeCount).validate.nullTest == "error")&& (rgEmpty.test(oFields.item(nNodeCount).rawValue) ){bPass = false;break;}}} if(bPass)... Do lock and submit ...

None of this code is tested. I just made it up. But it should do the trick.

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

brzap
Registered: Jan 27 2010
Posts: 6
Thanks thomp. Sorry that I didn't provide all of the information from the get-go regarding that I'm using LiveCycle Designer. To be honest, I had no idea that I was. I'm completely new to this type of stuff.

I pasted your script into my button. Here is what I have as a "pre-submit" event in my submit by email button:

var rgEmpty = /^\s*$/;
var bPass = true;
for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
var oFields = xfa.layout.pageContent(nPageCount, "field");
var nNodesLength = oFields.length;
// Set the field property.
for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
if ( (oFields.item(nNodeCount).validate.nullTest == "error")
&& (rgEmpty.test(oFields.item(nNodeCount).rawValue) )
{
bPass = false;
break;
}
}
}

if(bPass)
for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {
var oFields = xfa.layout.pageContent(nPageCount, "field");
var nNodesLength = oFields.length;
// Set the field property.
for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {
oFields.item(nNodeCount).access = "protected";
}
}

When I open the attachement from the email it isn't locked. I may have something completely wrong. Can you review it and let me know if I'm missing something?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
A syntax error should have been reported. It looks like there is a missing "{". Did you check the console window?

if the validation doesn't pass, then it shouldn't be sending the form. Where is this script being called and what mechanism are you using to email?

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

brzap
Registered: Jan 27 2010
Posts: 6
I'm new to all of this and I'm not sure what the "console" window is however, I did not receive any "error messages." I am sending the email using the built in functionality of a submit button....I am not using any code other than putting "mailto:emailaddress [at] company [dot] com?subject=Form Name" in the Submit to URL box.Would it be more appropriate to send the email/attachment via "email code" instead of a submit
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I should have asked these questions up front. Let's start over.

Using the email submit button is perfectly fine, unless you want to do something that's out of the box. So what's in the box? For the submit action, Acrobat will refuse to submit the form until all required fields contain some kind of data.

So, for just submitting the data you don't need to have any test code as long as the required fields are marked. But it you want to do something out of the box such as locking all the fields for a successful submit, you need to write a script to perform all activities. You can't use the built-in submit any more. There are articles on this site and forum threads that cover every aspect of what you want to do. This is a common topic.

There are links in my signature block that explain the Console window. This is the most important tool in Acrobat for developing JavaScript code. And it's a good place to start.

Search the site and the forums for "Validation", "nullTest", "email", and "submit". You'll find plently that will help you along.

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

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ok, I went back and tested the validation code I posted earlier and fixed it up. This code is performing exactly the same check that is done on submit. Just making sure that all required fields are filled in. Use it exactly like you were in the code you posted. You can use it on the same submit button you've been using, Put it into the pre-submit event.
var rgEmpty = /^\s*$/;var bPass = true;for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++) {var oFields = xfa.layout.pageContent(nPageCount, "field");var nNodesLength = oFields.length;// Set the field property.for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++) {var oFld = oFields.item(nNodeCount);if ( (oFld.validate.nullTest == "error")&& ((oFld.rawValue == null) || rgEmpty.test(oFld.rawValue) ) ){bPass = false;break;}}}

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