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

Locking part of a form

Explorer ,
Jan 20, 2024 Jan 20, 2024

long-time reader, first-time poster. I will be the first to admit that I'm over my head. Here is the Reader's Digest version, my employer has tasked me with remaking all our forms and turning them into interactive PDFs. Most of the forms are pretty straightforward and I have had little issues. The main form is currently 4 pages with information from the first two pages populating information on the last two. I  have come up with this after many searches:

 

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

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ((typeof f.page=="number" && f.page==0) || (typeof f.page=="object" && f.page.indexOf(0)!=-1)) f.readonly = true; // makes only fields on page 1 readonly

}

 

I don't remember exactly which posts helped but Try67 and a few others have answered similar questions with variations of the above script and that helped. This script made the first page of the original 3-page version of the form read-only but now they would like it to be able to turn on and off the read-only for the first two pages. Any help would be appreciated. 

Nate

TOPICS
JavaScript , PDF , PDF forms
1.2K
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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 22, 2024 Jan 22, 2024

Use this in checkbox:

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

if(f.name !== event.target.name) {
if((typeof f.page === "number" && (f.page === 0 || f.page === 1)) || (typeof f.page === "object" && (f.page.indexOf(0) !== -1 || f.page.indexOf(1) !== -1))) {
f.readonly = event.target.value === "Off" ? false : true;}}}

View solution in original post

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
Enthusiast ,
Jan 21, 2024 Jan 21, 2024

Where do you use script?

If it's a button, will that button be on one of the first two pages?

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 ,
Jan 21, 2024 Jan 21, 2024

I was thinking of using the script with a checkbox on either page one or two, preferably page one. What I'm trying to do is make it harder to accidentally change information on the first two pages. The first two sheets are used by the office with the customer while figuring out what they want for their cabinets and it can change multiple times before things are finalized, and can even change while the cabinets are being built, the last two are to be used by the rest of the shop.

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 ,
Jan 21, 2024 Jan 21, 2024

So you want to toggle all fields at the same time (ie. change their state from the current one to the opposite), or to set them all as read-only or as editable?

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 ,
Jan 21, 2024 Jan 21, 2024

Yes, I want to be able to toggle the fields on the first two pages as needed between read-only and editable.

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 ,
Jan 21, 2024 Jan 21, 2024

Try this code:

 

for (var i = 0; i < this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if ((typeof f.page=="number" && (f.page==0 || f.page==1)) || (typeof f.page=="object" && (f.page.indexOf(0)!=-1 || f.page.indexOf(1)!=-1))) 
		f.readonly = !f.readonly;
}
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 ,
Jan 21, 2024 Jan 21, 2024

Since it will be a toggle, you should also add condition to not set field (checkbox) to read only in which is script used since checkbox will be used in one of the pages that will be set 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
Explorer ,
Jan 21, 2024 Jan 21, 2024

I saw a script that you posted a few days ago that allows for the toggle but for all pages. I'm still lost on how to make this 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
Community Expert ,
Jan 22, 2024 Jan 22, 2024

Use this in checkbox:

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

if(f.name !== event.target.name) {
if((typeof f.page === "number" && (f.page === 0 || f.page === 1)) || (typeof f.page === "object" && (f.page.indexOf(0) !== -1 || f.page.indexOf(1) !== -1))) {
f.readonly = event.target.value === "Off" ? false : true;}}}
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 ,
Jan 22, 2024 Jan 22, 2024

I think I see where i was going wrong. When I would change the script I was locking the last two pages, then I would attempt something else and part of the front page would disappear. Thank you for your help. It works beautifully.

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 ,
Jan 21, 2024 Jan 21, 2024

so this script makes the first two pages read-only but will not allow it to go back to editable. I have tried this code

 

for(var i = 0; i < this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if(f.name !== event.target.name){
if(event.target.value == "Off")
f.readonly = false;
else
f.readonly = true;}}

 

by  Nesa Nurani from the discussion "Script to lock all editable fields in PDF, then deletes the "Lock" button." This script allows for the turning on and off the read-only. I have tried some variations of the two scripts with little success.

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 ,
Jan 22, 2024 Jan 22, 2024
LATEST

I would like to thank try67 and Nesa Nurani for their help. They were both extremely helpful and came up with the solution to a problem I have been working on for a few weeks, should have asked for help sooner but was trying to learn (pretty stubborn). Thank you again

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