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

Javascript formula

Community Beginner ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

I need to translate the following into javascript for a pdf form I am working on that calculates monthly costs based on two different rate factors. 

 

Can you help translate the following into javascritp?

 

if (cost) is < or = 130000 multiply by .02037977 else if cost is > 130000 but < or = 260000 multiply by .02037981

 

Thanks in advance for your help.

 

TOPICS
How to , JavaScript

Views

3.0K

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
3 ACCEPTED SOLUTIONS
Community Expert ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Then try this as 'Validation' script of that field:

if(event.value == "")event.value = 0;
else if(Number(event.value) > 0 && Number(event.value) <= 130000)
event.value = Number(event.value)*.02037977;
else if(Number(event.value) <= 260000)
event.value = Number(event.value)*.02037981;

View solution in original post

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

> You can't have space in variable name.

View solution in original post

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Like this:

 

if(event.value == "") this.getField("NEXT60MOS").value = 0;

else if(Number(event.value) > 0 && Number(event.value) <= 130000)
this.getField("NEXT60MOS").value = Number(event.value)*.02037977;
else if(Number(event.value) <= 260000)
this.getField("NEXT60MOS").value = Number(event.value)*.02037981;

View solution in original post

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

In order to assist you better you must define a few more details.

 

For example, what is (cost) ? 

 

Is this value taken from a selection that a user choose from a dropdown menu? 

 

An export value that you whish to factor in in your equation when the user ticks a checkbox or radio  button?

 

Or is it a numerical value that the user types in in a texfield object?

 

And what do you want the field that will display the total to do when the (cost) is blank or null?

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 Beginner ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

Thanks for your quick reply. The Cost will be a numerical value that the user would type into a texfield.

 

If that field is left blank or is 0 then the total field I would want to show 0 as well.  

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

If I understood correctly, you need the user to type in a numerical value in the designated (cost) field.

 

You may use different methods with Acrobat JavaScript, for example, a Validation script, a Custom Format script, or a Custom Keystroke script (among many other approaches)

 

I you are new to Acrobat JavaScript, the quickest and simplest way (in my personal opinion) is to do this as a custom calculation script.

 

Below is an example that will execute the script from the event target field (cost).

 

The script declares a variable for the event value (the multiplicand in this case).

 

And it also performs a conditional comparisson of the values entered in that field.

 

If the event value is 0 or the user deletes the numerical value it will display a value of 0 automatically.

 

Then, if the event value is equal or less than 130000 or if it is greater than 130001  the script will execute the desired factorisation and apply the desired multiplier to satisfy that condition.

 

See below:

 

var cost = event.value;

if(cost =="" && cost ==0) event.value = 0;

else {(cost <= 130000 && cost >= 130001) ? event.value = (cost * .02037977) : event.value = (cost * .03037981)}

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

Here is the corrected script.

 

Let us know if this works.

 

var cost = event.value;

if(cost =="" && cost ==0) event.value = 0;

else {(cost <= 130000 && (cost >= 130001 && cost <= 260000)) ? event.value = (cost * .02037977) : event.value = (cost * .03037981)}

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

You have few errors in this approach.

1. It will always calculate a second condition

(cost <= 130000 && (cost >= 130001 && cost <= 260000)

The number can't be less than 130000 and more at the same time, so first condition is false and it only calculates the second condition.

2. The number in second condition is wrong, it should be .02037981 not .03037981.

3. If you use this as a calculation script, it will change every time you change something in the form.

Also, no need to check for 0 and set it as 0 since it's already 0 🙂

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

Thank you Nesa Nurani.

 

I definitely missed a lot there.

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

Or when the cost is >260000?

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 ,
Nov 07, 2022 Nov 07, 2022

Copy link to clipboard

Copied

Try67,

 

Yes I forgot that part in the script that I just posted.

 

Thank you for catching that

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Thank  you very much for your help guys, this is all way above my head. Let me find out what the multiplier is if the cost is over 260000.  Once again I appreciate all of your input here.

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

You're welcome.

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

OK we dont have to worry about anything over 260000 so I will go with the following and let you know how it goes.   

 

defaulthv1qy9nywblw_0-1667920696296.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
Community Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

The cost field is named CARM COST and when I put in the formula below I got the error listed below :

defaulthv1qy9nywblw_0-1667921594512.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
Community Expert ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

You can't have space in variable name.

Is "CARM COST" field where you wish to have calculation, or it's a different field?

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

"CARM COST" is the name of the text field where the cost is entered.

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Then try this as 'Validation' script of that field:

if(event.value == "")event.value = 0;
else if(Number(event.value) > 0 && Number(event.value) <= 130000)
event.value = Number(event.value)*.02037977;
else if(Number(event.value) <= 260000)
event.value = Number(event.value)*.02037981;

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

What did I do wrong here?

defaulthv1qy9nywblw_0-1667923893384.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
Community Expert ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

> You can't have space in variable name.

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

So should I change the name of the field as well (to CARMCOST)?

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

No, the two don't need to match.

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

no mess with the field namenif you're running the script in the target field itself (where the user will interact typing in a value and displaying the result in that same field)

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

you can name your variable like:

 

carmCost = event.value;

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 ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Why do you keep adding that variable? Just use the script I gave you without changing anything.

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 Beginner ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

Tried it with the first line in or out and neither are calculating correctly:

defaulthv1qy9nywblw_0-1667925982756.png

defaulthv1qy9nywblw_1-1667926022165.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
Community Expert ,
Nov 08, 2022 Nov 08, 2022

Copy link to clipboard

Copied

You need to pay attention to what the scripts offered here are meant to do.

 

When you first asked in the OP you said that  the (cost) field will allow the users to type in a numerical value in it and to display the result in that same field.

 

So now, in your slide you have two fields: CAM COST which is where I see the users will enter a numerical dollar value and the other field MONTHLY PAYMENTS NEXT 60 DAYS... which is a very long name for a text field object.

 

The script needs to be slightly modified to display the calculated value in that other field.

 

 

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