Skip to main content
October 8, 2014
Answered

PDF Form javascript for making readonly field by using button

  • October 8, 2014
  • 2 replies
  • 47151 views

Please let me know the PDF Form JavaScript for making selected fields(Text field,Drop down list,..) as read-only by using Button.

This topic has been closed for replies.
Correct answer gkaiseril

Can you please explain how to do this? I have searched creating a loop and I have found a few different articles but I can't find the proper code to insert into the javascript box that comes up with the button.


Do you want your button to be made read only?

One starts with Acrobat JS Reference.

// make all fields in a form read only;

var oField; // variable for field being processed;

// loop through the form fields;

for (var i = 0; i < this.numFields; i++) {

// process each field name;

oField = this.getField(this.getNthFieldName(i)).readonly = true;

}

2 replies

Participant
July 18, 2017

This is a great thread..I've learned a lot, but I haven't been able to solve my problem...sorry I'm a JS newbie.

I have a form with a reset button. Several calculated fields are marked as read only. I’m having trouble with the actions a “Clear Button” executes in the form.

Under the Button Properties \ Actions tab if have two actions associated with the button – the first is a JavaScript that does the following:

for (var cntr = 0;cntr < this.numFields;cntr++)

{

this.getField(this.getNthFieldName(cntr)).readonly = false

};

     this.getField("SUBMITDT").readonly = true;

     this.getField("AGENCY").readonly = true;

     this.getField("EIN").readonly = true;

//this.getField("XORG").readonly = true;

//this.getField("EMPNAME").readonly = true;

     this.getField("GreyBox").readonly = true;

     this.getField("AUTH1").readonly = true;

     this.getField("AUTH2").readonly = true;

     this.getField("AMOUNT").readonly = true;

     this.getField("CONTROLNUM_PDF").readonly = true;

     this.getField("AUTHB").readonly = true;

     this.getField("TranCode1").readonly = true;

     this.getField("TranCode2").readonly = true;

     this.getField("TranCode3").readonly = true;

     this.getField("TranCode4").readonly = true;

     this.getField("TranCode5").readonly = true;

     this.getField("TranCode6").readonly = true;

     this.getField("TranCode7").readonly = true;

     this.getField("TranCode8").readonly = true;

     this.getField("TranCode9").readonly = true;

     this.getField("TranCode10").readonly = true;

     this.getField("MILERATE1").readonly = true;

     this.getField("MILERATE2").readonly = true;

     this.getField("MILERATE3").readonly = true;

     this.getField("MILERATE4").readonly = true;

     this.getField("MILERATE5").readonly = true;

     this.getField("MILERATE6").readonly = true;

There are more fields than this....

The second action is “Reset a form” – with all the fields in the form “checked”.

Here’s my problem – when the “Clear Form” button is pressed, it resets the form as expected, but all of the fields I expect will be set to “readonly” are now open for edit – and a user can type into the field.

Any suggestions on how to reset the form, and still have the readonly fields “protected”?

try67
Community Expert
Community Expert
July 18, 2017

That should not happen... The two things are not related.

What I would recommend you do is delete the second action and do it in your

script, instead.

Simply add this line of code at the end of it:

this.resetForm();

On Tue, Jul 18, 2017 at 8:53 PM, jg84996200 <forums_noreply@adobe.com>

Participant
July 18, 2017

Thanks for the quick replay try67.

I eliminated the second action - "Rest a form" - and replaced it with the "this.resetForm();" statement at the end of the JS code. However, now the form fields don't reset, and I'm getting the same result - with the read only fields - as I did before.

I inherited this form and code from another programmer.

From what I've read, a form reset should only affect the contents of a field, not it's attributes / settings, (i.e., "read only").

If I'm understanding the code correctly - it first set's the "read only" state of all of the listed fields to false (or no),

for (var cntr = 0;cntr < this.numFields;cntr++)

  {

    this.getField(this.getNthFieldName(cntr)).readonly = true

....and then using the counter loops it through the fields again and sets the read only state of the fields to the indicated "= true" (or yes).

I'm wondering...should the "reset form" come before the code that set's the read only state of the field to "no" and "yes"? Just a guess. Doesn't seem right - now that I'm writing it - seems that the state needs to be set to false before resetting the field and then setting the state back to "read only" to protect it again.

try67
Community Expert
Community Expert
October 8, 2014

this.getField("FieldNameHere").readonly = true;

October 9, 2014

Thanks Mr.Gilad D, its working fine.