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

Read only fields

Explorer ,
Jul 28, 2023 Jul 28, 2023

Copy link to clipboard

Copied

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

Views

560

Translate

Translate

Report

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

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

It locked all fields

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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

 

Votes

Translate

Translate

Report

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