Skip to main content
Inspiring
July 28, 2023
Question

Read only fields

  • July 28, 2023
  • 1 reply
  • 1707 views

I have a code to mark fields on my pdf as read only. I would like 3 of the fields to remain active if they are empty. How do I adjust the code to exclude 3 fields if they are empty?

var currentPageNum = event.target.page;

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

  if (GetFirstFieldPage(currentField) <= currentPageNum){
 currentField.readonly = true;
 currentField.display = display.visible;
    }
  }

I tried the changes below, but I couldn't get it to work.

var currentPageNum = event.target.page;
var activeFields = ['field1', 'field2', 'field3']; // Names of the fields that should remain active if empty

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

  if (GetFirstFieldPage(currentField) <= currentPageNum) {
    var fieldValue = currentField.value;
    var isEmptyField = fieldValue === null || fieldValue.trim() === '';

    // Check if the field should remain active if it's empty
    if (activeFields.includes(currentField.name) && isEmptyField) {
      currentField.readonly = false;
      currentField.display = display.visible;
    } else {
      currentField.readonly = true;
      currentField.display = display.visible;
    }
  }
}
This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
July 28, 2023

Are you getting any error messages? Some of the methods you're using will not work in earlier versions of Acrobat/Reader (such as trim and includes).

Inspiring
July 28, 2023

No error message.  None of the fields change to read only.

try67
Community Expert
Community Expert
July 28, 2023

Try to simplify your code. You have a bunch of conditions and functions we can't see and don't know if they work or not. Also, you need to move the definition of the isEmptyField variable outside of the if-statement where it's currently located.