Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Read only fields

Explorer ,
Jul 28, 2023 Jul 28, 2023

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;
    }
  }
}
TOPICS
JavaScript , PDF
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2023 Jul 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2023 Jul 28, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2023 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2023 Jul 28, 2023

This code should make all fields, except for those in the list, read-only:

 

var activeFields = ['field1', 'field2', 'field3']; // Names of the fields that should remain active if empty
for (var i = 0; i < this.numFields; i++) {
  var fname = this.getNthFieldName(i);
  if (activeFields.indexOf(fname)!=-1) continue;
  var currentField = this.getField(fname);
  currentField.readonly = true;
}

 

Does it work?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2023 Jul 28, 2023

It locked all fields

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2023 Jul 28, 2023

Then the field names in the array are incorrect. Remember that JS is case-sensitive, and that the names must match EXACTLY, so if the fields are actually called "Field 1" or "field1" instead of "field 1", it won't pick it up.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2024 Apr 13, 2024

Does anyone has the solution, every eclusion ("All fields except these"  and" Just these fields" doesn't work either by providing a script or working pdf 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2024 Apr 14, 2024

The script provided above by try67 will set the fields to read only, except the ones you put in array.

Did you try that script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2024 Apr 14, 2024

Yes, I did.. All fields are turning to read only. Here is the file with excution of the script on employee signature. What am I missing?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2024 Apr 14, 2024

Works fine for me, all fields are read only except the 3 fields from the script.

You don't need script for that, try these options:

tempsnip.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2024 Apr 15, 2024
LATEST

I did that too, it doesn't work for me

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines