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

Custom Calculation Script vs JavaScript Action

Community Beginner ,
Jun 12, 2018 Jun 12, 2018

Copy link to clipboard

Copied

Hello

I want to create a PDF document that only allows the user to edit text fields if they are blank. Once the field is not blank it should not be editable (read-only). This is the code i used for that:

{

var f = this.getField(event.target.name);

if (f.value == "")

f.readonly = false;

else

f.readonly = true;

}

I also have a button that asks for a password, and if the password is correct they can edit a field until they click out of the field.

My issue is that the above code only works if the custom calculation script is set to this. If I take the same code and add a Mouse Up JavaScript Action it does not work correctly. For example if a field is blank i have to click on the field then click out and then click on it again to enter a value. Is there way to focus on the script once i click on it?

The reason I want to have the script in a action rather than a custom calculation script is that i have many fields and pdfs that need to modify. So instead of going to each field individually and pasting the script in the custom calculation script property field i can add select many fields and set the javascript and it will propagate to all the fields selected.

Thanks

Please let me know if i should clarify

TOPICS
Acrobat SDK and JavaScript , Windows

Views

961

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
Engaged ,
Jun 13, 2018 Jun 13, 2018

Copy link to clipboard

Copied

I am not quoite sure I'm getting what you are trying to achieve but I will help you sort things out.

Are you sure you want the field to lock as soon as a value is commited to it.  What if the user makes an error?  Will he have to start all over again?

Using a calculate script, you would not have to put the script in every field manually.  A calculate script runs each time a value is changed in your document.  The script you wrote would best be used as a validation script, and it would have to be entered manually for every field unless you use a document-level script.

But since you used a calulate script, you only need to have it at one place, but target all the fields instead of event.target  .

var myFields = ["field 1", field 2", "field 3"]  //add them manually or through a loop

for (i in myFields){

    var f = this.getField(myFields);

    if (f.value == "") f.readonly = false;

    else f.readonly = true;

}

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 ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

Hi,

If you use the code above in a calculation script, would it not make all the fields you haven't got to yet ( assuming they are blank to start with) read only before the user gets to fill it in?

User enters field1 - types some text "myName"

Calculate runs

field2 and field3 that don't have any text become read only.

This would probably need to be on each fields validate ( or calculate) event, but you could store the function globally as a document script which you could then call.

Something like

function makeReadOnly ( fieldName)

{

     var f = this.getField (fieldName);

     if ( f.value !== "")

     {

          f.readonly = true;

     }

     else

     {

          f.readOnly = false;

     }

}

then on each field you could just call

makeReadOnly ( event.target.name);

Then if you had to change anything in the function you only have to do it at one place.

Hope this helps

Malcolm

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
Engaged ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

BarlaeDC  a écrit

Hi,

If you use the code above in a calculation script, would it not make all the fields you haven't got to yet ( assuming they are blank to start with) read only before the user gets to fill it in?

No because the value of the fields the user have not reach is still "", thus triggering the if statement I provided.

BarlaeDC  a écrit

This would probably need to be on each fields validate ( or calculate) event, but you could store the function globally as a document script which you could then call.

Your script, document level script would work great as a validate script as you said, but not as a calculate script.  Although it would produce the same result in the end, you need to remember a calculate script runs EVERY TIME a value is commited, regardless of where it was commited from.

So to quote you again, if the user enters a value to field1, field2 and field3 will run their own calculate script also, for nothing.  When only 3 fields are involved, it's no problem.  But when you have many it can slow everything down.  A script should run only when needed to.

In my opinion, the doc-level function/validate event combo is the best solution but just a bit more complicated to understand for a begginer.

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 ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

LATEST

Hi,

MatLac​, Sorry I missed that if the first time round, you are correct your code would word as expected.

In my opinion, the doc-level function/validate event combo is the best solution but just a bit more complicated to understand for a begginer.

I agree it is tougher to learn but it is probably a better habit to get into as it tends to keep your code in the forms easier to find and fix.

(and I include calculate for completeness, validate tends to be the better option for most scripts)

Regards

Malcolm

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