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

Create custom button to lock fields...

LampchenKo
Registered: Oct 17 2011
Posts: 16
Answered

How can I create a custom button that has the function to lock the fields in the form, just like the signing option does? This will not sign the document, just lock the fields, and it does not have to be password protected...
 
I have posted some details, but I will upload the form later so everyone can see the final result... Thanks

My Product Information:
Acrobat Pro 10.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Is this file going to used in Acrobat or in Reader?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

LampchenKo
Registered: Oct 17 2011
Posts: 16
Hi... In Reader, I'm the only one with Acrobat right now...
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
In that case you can't really lock the fields, but you can make them read-only, with this code:
  1. for (var i=0; i<this.numFields; i++) {
  2. var fname = this.getNthFieldName(i);
  3. var f = this.getField(fname);
  4. if (f.type!="button") f.readonly = true;
  5. }

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

LampchenKo
Registered: Oct 17 2011
Posts: 16
Works perfect... Just one more question, how can I add exceptions on the script? I need my two signatures to stay available... Thanks
UVSAR
Expert
Registered: Oct 29 2008
Posts: 1357
LampchenKo wrote:
Works perfect... Just one more question, how can I add exceptions on the script? I need my two signatures to stay available... Thanks
Change line 4:

if (f.type!="button" && f.type!="signature") f.readonly = true;
CompWiz32
Registered: Dec 12 2011
Posts: 9
I am building an AcroForm and have only basic Javascript knowledge. Could someone guide me how to create an exception for fieldnames beginning with a certain text?

For example, I built a form with two sections. The upper section (PART1) gets populated with existing data by admin staff and then gets sent to the end user for review. The end user reviews Part1 data and lists any changes in the lower section (PART 2).

I was hoping to build a button that would lock all fields in part 1 before distributing to the end user. This would make it so that the only fields the user can change are part 2 fields. To make this easy I started all fieldnames with Part1 or Part2.

I would like to only lock the fields whose fieldname begins with Part1.


George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
If you use a hierarchical naming scheme (PART1.text1, PART1.text2, PART1.text3, etc.), you can control all of the fields with the same prefix with a single statement. For example:

// Sett all of the PART1 fields to read-only
getField("PART1").readonly = true;
try67
Expert
Registered: Oct 30 2008
Posts: 2398
If you're not using George's method, you can add something like this to the code I posted earlier

if (/^PART/.test(f.name))
f.readonly = true;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

CompWiz32
Registered: Dec 12 2011
Posts: 9
I am having some issues getting it to work correctly. Maybe I missed something basic.... Could someone check out my work and see if I made a mistake?

Field Names (three of many):
Part1.NetworkLocation
Part1.ResourceOwner
Part1.GroupName


Button action to lock text:
Mouse Up

Code bound to button:
for (var i=0; i
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Place your code inside a < code > tag for it to post properly. See here: http://acrobatusers.com/filter/tips

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

CompWiz32
Registered: Dec 12 2011
Posts: 9
  1. for (var i=0; i<this.numFields; i++) {
  2. var fname = this.getNthFieldName(i);
  3. var f = this.getField(fname);
  4. if (/^PART/.test(f.name)) f.readonly = true;
  5. }
  6. <c>
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Seems fine to me... Are you getting any error messages in the console?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

CompWiz32
Registered: Dec 12 2011
Posts: 9
No error messages that I can see. However, I have one question?

How does the above code differentiate between fields starting with Part1. or Part2. ?

try67
Expert
Registered: Oct 30 2008
Posts: 2398
It doesn't. It will make all fields starting with "PART" read-only.
However, this is case-sensitive, so if your field-names do not start with "PART" but with "Part" then it won't work. You'll need to edit it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

CompWiz32
Registered: Dec 12 2011
Posts: 9
try67 wrote:
It doesn't. It will make all fields starting with "PART" read-only.
However, this is case-sensitive, so if your field-names do not start with "PART" but with "Part" then it won't work. You'll need to edit it.
Ok - Makes Sense so if i change the code to:

if (/^PART1/.test(f.name)) f.readonly = true;
That should work if my fields are called PART1.fieldname1, PART1.fieldname2, etc. Right?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
No... Change it to:
if (/^Part/.test(f.name)) f.readonly = true;

Or if you want to make it case-insensitive:
if (/^part/i.test(f.name)) f.readonly = true;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

CompWiz32
Registered: Dec 12 2011
Posts: 9
try67 wrote:
No... Change it to:
if (/^Part/.test(f.name)) f.readonly = true;

Or if you want to make it case-insensitive:
if (/^part/i.test(f.name)) f.readonly = true;
Ok - changed my code to

if (/^part1/i.test(f.name)) f.readonly = true;
and it looks like it works now!

CompWiz32
Registered: Dec 12 2011
Posts: 9
New wrinkle....

Form seems to work fine... however, when I distribute the form via FORMS>DISTRIBUTE I now get an error that says "Failed to send form to Recipients" and in the bottom corner there is a progress bar that says "Extending Features in Acrobat Reader". The progress bar disappears when I click OK on the error messageIt feels like somehow when I made the fields read only, i inadvertently broke the the ability to have Adobe prepare the document for mail sending. I can confirm that all the background stuff like mail connectivity and email address are correct and working.

Any ideas?


CompWiz32
Registered: Dec 12 2011
Posts: 9
Fixed the issue. Found this thread

http://acrobatusers.com/forum/forms-acrobat/failed-send-form-recipients-form-distribution-aborted

Which explains that I had selected the Remember my choice option. Unselected and form distribution worked.
CompWiz32
Registered: Dec 12 2011
Posts: 9
Just wanted to take a moment to say thank you to "Try67" and "George_Johnson" for the help. Would not have been able to do what I needed without your help. The time is much appreciated.