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

Make "ReadOnly" any field with data in it

Chocko
Registered: Mar 19 2009
Posts: 14

I've done extensive searching on the net as well as browsed these forums quite a bit, so I think I'm asking a question that isn't often asked.

Sorry if I didn't see an original thread and this is a repost!!

With the click of a button using Javascript, I'm trying to change the properties of any field without a null value to Read Only.

As I don't know the command for "all fields", I started off working with a specific field.

This is what i have so far, but doesn't seem to work:

var f = this.getField("A 25");
var bLock = (event.value != "");
f.readOnly = bLock;

What am i missing? and does anyone know how i could recode the first like so that it applies to all fields?

Thanks,

Chocko

Chocko
Registered: Mar 19 2009
Posts: 14
double post
Chocko
Registered: Mar 19 2009
Posts: 14
I was able to figure this out, if anyone's curious--here's what I did:

{
var f = this.getField("Day");
if (f.value == "")
f.readonly = false;
else
f.readonly = true;
}

This works like a charm, but there is a simple redundancy problem. As I don't know how to get that first line to apply to any field in the pdf, i have MANY lines of code.

Can anyone help me simplify this?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
With AcroForms you will need to loop through all form fields. You can use the "numFields" property to obtain the number of form fields and then use a control loop and use the "getNthFieldName()" method to get each field's name.
var iFieldName; // for nth field namevar oFieldName; // object for iFieldName// loop through fields or formfor(i = 0; i < this.NumFields; i++) {iFieldName = this.getNthFieldName(i); // get i field nameoFieldName = this.getField(iFieldName); // object for i field nameoFieldName.readonly = false; // assume r/w// process non-button fields only, buttons do not have a field valueif(ofieldName.type != "button") {// test non-button field for a valueif(oFieldName.valueAsString != "") {oFieldName.readonly = true; // has data so r/o} // end if value not empty} // end if non button type} // end loop through fields

George Kaiser

Chocko
Registered: Mar 19 2009
Posts: 14
gkaiseril wrote:
With AcroForms you will need to loop through all form fields. You can use the "numFields" property to obtain the number of form fields and then use a control loop and use the "getNthFieldName()" method to get each field's name.
var iFieldName; // for nth field namevar oFieldName; // object for iFieldName// loop through fields or formfor(i = 0; i < this.NumFields; i++) {iFieldName = this.getNthFieldName(i); // get i field nameoFieldName = this.getField(iFieldName); // object for i field nameoFieldName.readonly = false; // assume r/w// process non-button fields only, buttons do not have a field valueif(ofieldName.type != "button") {// test non-button field for a valueif(oFieldName.valueAsString != "") {oFieldName.readonly = true; // has data so r/o} // end if value not empty} // end if non button type} // end loop through fields
Thanks for the reply! I'll see what i can get it to do.
Minh Duong
Registered: Oct 29 2010
Posts: 1
i made a button save as and it save to my server but i want after save all the fields become read only.
but when i try to put the code:

this.getField("lastname").readonly=true;
event.target.myTrustFunct(event.target); //this should execute the save as js file
event.target.closeDoc(true); //autocloses document if validation passesand it doesn't work!!!
but it works when:

event.target.myTrustFunct(event.target); //this should execute the save as js file
event.target.closeDoc(true); //autocloses document if validation passesplease so me my problem and how can i make the field readonly???
Thanks