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

Protect field with javascript?

Formfingers
Registered: Feb 7 2007
Posts: 90
Answered

I've been told that there is no "official" way to have a particular field(s) password protected within a form - but that you can mimic something similar using scripts instead.

Does anyone know/can post a script that would allow particular fields within a form to be unaccessable by anyone without a password to activate the field? I've received a few samples on doing this in the Click Event of Designer, but those samples haven't been helpful.

Again, when you click the field, it initiates a "password" (or prompt box) that a person must enter into to activate the field. Any suggestions would be GREATLY appreciated!!

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Field access is controled through the "access" property. You'll find info on this in LiveCycle "XML Form Object Model Reference".

To display a popup Box for entering passwords you could use either custom dialog, which requires some work and JavaScript expertise, or the much simpler "response" box from Acrobat JavaScript.

app.response(...);

You'll find info on the response box in the Acrobat JavaScript Reference.

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

Formfingers
Registered: Feb 7 2007
Posts: 90
Thanks thomp - A few more questions for you...

I've got my response box set up; pops right up when I click on the field. However, I need that field to become locked or unlocked depending on the "password" that a user enters. Right now, I've set access to protected and set up an if/else statement to be either readOnly or open....but I can't get this statement to tie into the "password" that a user enters.

Have I set this up correctly or am I missing something entirely? Here's what I have below, on my enter event:

app.response("Please Enter Password:", "Document Control Only");

SCPONo.access = "protected";

if (event.target.rawValue =12345) {
form1.SCPONo.access = "open";
}
else {
form1.SCPONo.access = "readOnly"
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
This process is a bit more complex than it would first appear. But to begin, lets go over your code.

1. The "app.response" function returns a value. It's this value that needs to be used in the if statment.

2. "event.target" references the AcroForms document object, so it doesn't belong in this context.

Your code should look something like this.

var cRtn = app.response("Please Enter Password:", "Document Control Only");

if(cRtn == "12345")
{
SCPONo.access = "open"
... MOre Code ...
}


Next, this code does not belong on the text field you want to protect. The field should be protected to begin with, which means that is does not recieve mouse events. If you leave it unprotected, and set the "access" to readOnly, there is no way to give the user another try to set the password.

Instead, cover the field with a transparent button, no border and no fill color. When user click on the field they are really clicking on the button. Put your code in the button, If they enter the correct password, then remove the button and enable the text field. Like this.

if(cRtn == "12345")
{
SCPONo.access = "open";
this.presence = "hidden";
xfa.host.setFocus("xfa.form.form1.SCPONo");
}


I haven't tested any of the code, but I believe that this strategy will work.

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

Formfingers
Registered: Feb 7 2007
Posts: 90
Thanks again Thomp for the great suggestion! I've gone through and set up the button/field to relate with each other, and it works great!

My final three questions (and hopefully these are simple fixes):
1) A user can get around the password prompt of the hidden button by simply using their tab key to enter the field. Any ideas on how to block this or tab over the field?

2) I must not truly have this field protected. Can you break down where I need to edit the default "open", to change this to "protected"?

3) Is there a way to edit the default browser text on the pop-up, where it says "Warning: Javascript Window - Document Control..." I'd like to get rid of the "Warning: Javascript Window" part.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
1) If the signature field is set to ReadOnly, the user can't touch it.

2) On the "Object->Value" tab, set the "Type" to "Read Only"3) This warning is displayed when the response box is displayed form an un-privileged contect. For a document script there is no pratical way to get rid of it. It's part of the Acrobat Security Model.

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

tracym
Registered: May 18 2008
Posts: 7
How does this work for mulitple fields to add focus? I have an "office use only" section in my fillable form. I followed your procedure above and it works great but I have 6 fields and don't want admins to enter a password for each field. Is there a way to have it so the read only fields under office section will set focus when any one read only field password is entered? I tried the below on each button I placed over the text field, but it's not working:

var cRtn = app.response("Please Enter Password:", "Document Control Only");
if(cRtn == "12345")
{
field1.access = "open";
this.presence = "hidden";
field2.access = "open";
this.presence = "hidden";
field3.access = "open";
this.presence = "hidden";

xfa.host.setFocus("field1,field2,field3,etc.");
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The presence property for the button only needs to be set to hidden once. But, instead of placing a button over a single field, place it over the entire section. If the button is at the same hierarchy level as the other fields, then the code above should work.

As for the setfocus fucntion, you can only set focus to a single field at a time. It doesn't make sense for more than one field to have focus. You'll also need to use the entire SOM path in this function, not just the field name.

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

tracym
Registered: May 18 2008
Posts: 7
Using the below code, it removes the read only but the button won't go away...if I click the button (that's covering the section), and go to enter information, I get prompted again for the password. I made a smaller button that doesn't cover the fields - that's how I can enter information. How can I make the button go away after entering the password? What's an SOM (sorry, new at this)? Thanks! :-)


var cRtn = app.response("Please Enter Password:", "Document Control Only");
if(cRtn == "12345")
{
field1.access = "open";
field2.access = "open";
field3.access = "open";
this.presence = "hidden";
}
tracym
Registered: May 18 2008
Posts: 7
I made the button visible in designer but changed the presence to invisible rather than hidden and it seems to work okay:


var cRtn = app.response("Please Enter Password:", "Document Control Only");
if(cRtn == "12345")
{
this.presence = "invisible";
field1.access = "open";
field2.access = "open";
field3.access = "open";
}

Some help:

- the form doesn't save the javascript settings when submitted (will not re-set to prompt for password on office use only fields - all fields are open).

I have the form set (using a function) so once the form is filled in by a user, the completed form can be re-submitted (with user's previous entered information already filled in) annually to update information.

The office only fields will be already completed as well from the previous year, but I still want it to be read only for the user and edited only by an Administrator who has the password. It's okay if they see the information in the office only field as long as they can't edit or change it.

- Will users with the full version of abode be able to see the password if they open the form in designer?
tracym
Registered: May 18 2008
Posts: 7
Okay, I think I need this in a js file (hence hiding the password) and call function from the form button. Can you provide an example of how I can do this?
Example -

js file:

var cRtn = app.response("Please Enter Password:", "Document Control Only");
if(cRtn == "12345")
{
this.presence = "invisible";
field1.access = "open";
field2.access = "open";
field3.access = "open";
}

How would I call it from the button...myfunction(cRtn)? Thanks again, please excuse a novice.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You can't completely hide the password from a sophisticated user with Acrobat Pro. But you can apply Reader Enabling rights and security that will make it impossible for all but the most sophisticated users.

Using a folder level script to test the password won't help since anyone who could get access to the password could also just change the fields to "open" access without it.

Somethings in LiveCycle have a state that can be saved with the document and other things do not. Form field values, and visibility, are saved with the document. Since you don't want this state saved, this is a problem.

However there are two simple solutions:

1. Force the parameters to be in the desired state with the initialize event on the button.

2. Use the Pre-Save event to restore the visibility of the button.

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

tracym
Registered: May 18 2008
Posts: 7
Awesome - that works, thanks!

on click event (I only wanted it prompt if particular section was clicked - putting in inialize event prompted for password when document opened):

var cRtn = app.response("Please Enter Password:", "Document Control Only");
if(cRtn == "12345")
{

field1.access = "open";
field2.access = "open";
field3.access = "open";
this.presence = "invisible";

}


In PreSave event:
this.presence = "visible";

I'm not too worried about the password, but at least it's a deterrent. Thanks again!
cbatybarr
Registered: May 16 2010
Posts: 4
Thom, another question and please excuse my ignorance because i'm out of my league here, but can javascript only be used to simulate password protected fields in an editable pdf file if the file is being used online?

Again, I'd be building the file in Acrobat Pro and my client will be inputting her info into the active fields, then wanting to save the file from READER 8 to send as 'locked' to her clients so that they just print it out. Thanks!
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, as explained in the previous comments, the locked (i.e., read only) state of the document can be saved with the PDF.

However, it sounds like you want to document to always be locked, until the user enters a name and password? If this is true then the task is even easier. Set all the fields to Read Only to begin with. Then use the password entry to change them to "open". Reset to "readOnly" in the Pre-Save event so the form is always locked.

BTW: this is a Livecycle Form?

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

cbatybarr
Registered: May 16 2010
Posts: 4
Hello again, and thank you in advance.

No, this is NOT a LifeCycle form. I am just an average user working freelance graphic design, no programing knowledge.

Scenario:

1. I've created an editable pdf form in Acrobat Pro which I will send my client.

2. She will open the form with Acrobat Reader and fill in the form fields.

3. When she is done she will resave the pdf out of Reader to *email to her clients who we want to be sure will NOT be able to edit the form, but just print it out as a hard copy*

Can I set the password protected fields in Acrobat Pro, then send to client to use password to access fields, fill them in and then save the file as altered, then send on to another client only to open and print?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
First, this forum is specifically for LiveCycle forms, so you should have created a new topic in Forms.

You will definately need to do some programming to get this functionality. Since this form will be filled out by a single person, there really isn't a need for a password. Place a button on the form that contains a script for setting all the fields to ReadOnly and then hides the button. You can have the button also email the form.

Search the tutorials for articles on these topics. You should find some useful info.

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

samuel9294
Registered: Aug 10 2008
Posts: 1
""However, it sounds like you want to document to always be locked, until the user enters a name and password? If this is true then the task is even easier. Set all the fields to Read Only to begin with. Then use the password entry to change them to "open". Reset to "readOnly" in the Pre-Save event so the form is always locked.""


This is exactly what I want to do. Thomp I have a form with multiple fields plus one image field.
What I want is that when the form is distributed, the data can only be viewed, but with a password box that can enable edit access for all the fields.

Can u show me how that is done?

Thanks
Samuel
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
What you need is code that will loop over all fields in the form to switch the ReadOnly attribute on or off.

This could be activated from a "Lock/Unlock" Button. Assume that the form always starts off in the locked state. And you could enforce this by placing the same code in a top level Pre-Save event. The state should be saved in a member variable on the button object.

// Code for Lock/Unlock Buttonfunction LockForm(oExceptFld, bUnlock){var nPages = xfa.layout.absPageCount();for(var nPg=0;nPg<nPages;nPg++){var oFlds = xfa.layout.pageContent(nPg,"field");for(var i=0;i<oFlds.length;i++){// Exclude this buttonif(oFlds.item(i) != oExceptFld)oFlds.item(i).access = bUnlock?"open":"readOnly";} } } if(this.bUnlocked){this.bUnlocked = false;this.caption.value.text.value= "Unlock";LockForm(this,this.bUnlocked);}else{var cPswd = app.response("Enter Password");if(cPswd == "123456"){this.bUnlocked = true;this.caption.value.text.value= "Lock";LockForm(this,this.bUnlocked);}elseapp.alert("Bad Password");}

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

Registered: Jan 17 2011
Posts: 3
Hi,

not sure if after more than 3 years someone will look at this post, or should I have to open a new post.

I used this script in a request form that needs to be approved at different levels (manager, director, vp etc). Since usually there is more than one sales manger, I would like to provide each manager with his or her own password. I created a text field so the manager can enter his or her name. However, if without this variable the script is working perfect, I cannot make it working with the two variables. Below is my script - do you have an idea what I am doing wrong?


var cRtn = app.response("Please Enter Password:", "Document Control Only");
var lAppDSM = this.getField("app.Text8").value;


if (lAppDSM=="Peter" & cRtn=="12345")
{
this.presence="invisible";
}
else if (lAppDSM=="Peter" & cRtn!="12345")
{
app.alert("Incorrect password!}


Thanks




Mike

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I would look at using the network login name and the identity object of Acrobat. You can match the loginname of the identity object to your array of mangers in the form and if a match is found let them access the form or remove restrictions.

With a signature field using a certificate, one needs to provide a password to apply the signature.

George Kaiser

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Mike,

Are you working in Acrobat or LiveCycle Designer? The code you posted is a mixture of both.
Registered: Jan 17 2011
Posts: 3
I'm using LiveCycle Designer.

Mike

Registered: Jan 17 2011
Posts: 3
George, it's working now. Thanks for trying to assist on this.

Mike