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

Java Script possibly to lock only certain form fields when the document is encrypted

Community Beginner ,
Nov 05, 2021 Nov 05, 2021

So my document is 13 pages with approx 100-150 form fields in it and they are mainly made up of text boxes and check mark boxes, all of which have form field names. What I want to do is this, start with the blank unencrypted document and I will fill in some of the form fields myself. Now I want to do is encrypt the document and when I do that I would like for only some of the fields to be locked (mostly the ones I just filled in) so when I send it to an applicant they can only fill in the fields that are not locked and not be able to overwrite the fields I filled in.

I am tired of having to go thru the whole document and opening the properties of each field that I want to lock one at a time before I encrypt it. Is there any way of doing this?

TOPICS
How to , JavaScript , PDF forms
2.9K
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 ,
Nov 05, 2021 Nov 05, 2021

To make your fields (Text fields or checkboxes) readonly if they are filled you can use this script in a button as 'Mouse UP' event:

for ( var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if ((this.getField(fname).type == "text" && this.getField(fname).valueAsString != "")||(this.getField(fname).type == "checkbox" && this.getField(fname).valueAsString != "Off"))
this.getField(fname).readonly = 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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

To make your fields (Text fields or checkboxes) readonly if they are filled you can use this script in a button as 'Mouse UP' event:

for ( var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if ((this.getField(fname).type == "text" && this.getField(fname).valueAsString != "")||(this.getField(fname).type == "checkbox" && this.getField(fname).valueAsString != "Off"))
this.getField(fname).readonly = 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
Community Beginner ,
Nov 05, 2021 Nov 05, 2021

Thank you Nesa! I will try that. I am not to familiar with buttons or how to create one. I am really good at creating buttons in Excel so this cant be too difficult. Can you explain what exacactly 'mouse up" is. I have seen that option in the form fields and never knew what exactly that was. So I guess when I get a button created there will be that option as well in the properties of the button.

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 Beginner ,
Nov 05, 2021 Nov 05, 2021

Do I need to replace anything in that code with any form names or anything?

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 Beginner ,
Nov 05, 2021 Nov 05, 2021

Wow!! That is absolutley awesome!!

I do believe this will 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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Ok, now to expand on this just a little... how about a button with a code to unlock all fields regardless if they have text or not and regardless if the checkboxes are checked or not.

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Nesa, I am sorry but I just realized that there is also a few dropdown lists that needs to get locked with that code.

It just need to lock when I use the button. 

I tried to add another line for the dropdown but it is not locking.

I'm sure I dont have the syntax for "dropdown" correct.

what i have is this:

for ( var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if ((this.getField(fname).type == "text" && this.getField(fname).valueAsString != "")
||(this.getField(fname).type == "checkbox" && this.getField(fname).valueAsString != "Off")
||(this.getField(fname).type == "dropdown" && this.getField(fname).valueAsString != ""))
this.getField(fname).readonly = true;}

I changed the TRUE to FALSE to unlock the fields and put that in another button so that is good now.

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 ,
Nov 06, 2021 Nov 06, 2021

Sorry I couldn't respond sooner.

Do you still need help with anything?

For dropdown field use "combobox" not "dropdown".

Also for dropdown field don't use "" instead use  default value you have of dropdown, for example if you set empty space as default then use " "

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Oh no problem... 

I changed "dropdown" to "combobox" and that worked to lock it. Thanks!!

Now let me see if I can change the code on the unlock button to unlock it.

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

ok, lock and unlock is working great.

I have another question about case sense if you dont mind.

 

var PropertyAddress = this.getField("PropertyAddress ").valueAsString;
if (PropertyAddress =="")
event.value = ""; else if (PropertyAddress =="123 Main Street")
event.value = "Apple"; else event.value = "Ball"

This only works if user enters the text exactly.  I would like for it to not be case sensitive so that either

"123 main street" or "123 Main Street" or "123 MaiN StrEet" any other combination of case sense will work.  

 

Should I start a new thread for this...??

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 ,
Nov 06, 2021 Nov 06, 2021

See if this works for you:

var PropertyAddress = this.getField("PropertyAddress ").valueAsString;
var str = /123 Main Street/i;
var res = PropertyAddress.match(str);
if (PropertyAddress =="")
event.value = "";
else if (res)
event.value = "Apple";
else event.value = "Ball"

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Ok, But the "123 Main Street" is from the combobox which may have diff values...

well, if its a combo box then I should write what the combo box list is......hummmm 

let me think on this and play with it.. this may not be an issue now that im looking at it...

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Nesa, going back to the button that lock all the fields, I also created a button to unlock the fields so if I made a mistake I could quickly unlock it and make the correction and lock it back.

only one problem... when i encrypt the document, the unlock button is still clickable and the applicant could press that and undo all the locked fields. So I need to edit the code to also lock or "read only" the "unlock" button I created. The current code is on that button is:

 

for ( var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if ((this.getField(fname).type == "text" && this.getField(fname).valueAsString != "")
||(this.getField(fname).type == "checkbox" && this.getField(fname).valueAsString != "Off")
||(this.getField(fname).type == "combobox" && this.getField(fname).valueAsString != ""))
this.getField(fname).readonly = true;}

 

I would guess I would need to add another line to include all buttons to make them "unclickable" somehow...??

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 ,
Nov 06, 2021 Nov 06, 2021

You can set them to readonly also or hide them.

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 Beginner ,
Nov 06, 2021 Nov 06, 2021

Edit that...

If I set the buttons to "read only" in the button code then I wont be able to unlock anything and would have to go back into the properties to undo it. So, what I need is a way to have the button unclickable only when the doc is encrypted.

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 ,
Nov 06, 2021 Nov 06, 2021

Perhaps you don't need to make button readonly, use app.response to set password to unlock 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 Beginner ,
Nov 06, 2021 Nov 06, 2021

I can try that but I do not know know how to write that.

However it is written, would that be put in the doc java script?

In Tools/Javascript/Document JavaScript?

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 Beginner ,
Nov 19, 2021 Nov 19, 2021

This is a great thread and similar to a challange I am have.

My document as 45 pages with signatures between grouping of fields. Also mostly check boxes and text. When the signature on say page 6 get signed I want to lock all fields on pages 4 and 5 except for the other signature pages. Was thinking JAVA might be the solution to lock fields by pages with maybe an exception list for example lock all fileds on pages, 4 5 and 6 that are not signture 2 and checkbox 99.  Is this possible with JAVA because picking the field auto generated on 45 pages is not working 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
Community Beginner ,
Nov 09, 2022 Nov 09, 2022
LATEST

HI @Nesa Nurani  I have a question, can i encrypt text entred value using javascript and use anothercode to get back  to that text

thanks in advance.

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