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

# A LITTLE HELP ON A FORM FIELD CALCULATION

Community Beginner ,
Jun 27, 2024 Jun 27, 2024

Copy link to clipboard

Copied

HELLO ALL, I'M IN NO WAY A SCRIPT WRITER OR ANY KIND OF CODER. HOWEVER, THROUGH THE COMMUNITY I WAS ABLE TO COMPLETE ALOT MYSELF JUST BY PICKING THROUGH AND TESTING THINGS. I'M HAVING A LITTLE MORE TROUBLE WITH THIS ONE FIELD THAN I THOUGHT I WOULD.

RIGHT NOW I HAVE 1 FIELD "Test Number" which the value entered will display the cost in another filed named "Test Cost" (read only) which is also finalized with a simple calculation from anther field called "Test Cost2" + a field called "Rental Fee" which is just a standard number field the user inputs their rental cost. My problem is i have 2 other values i need to assign to the "Test Cost" (read only) field based upon the type of test being done. I have 2 specific test types that are a set fee and and i don't want the test fee to change based on the number entered in the "Test Number" field. If it's a school bus test i need the test fee to be set at \$100 no matter what the test number entered is. The same goes for an INDOT test, their fee's are set at \$75 no matter how many times they test.

HERE IS THE CALCULATION SCRIPT I HAVE CURRENTLY AND THIS WORKS FINE:

if(this.getField("Test Number").value == "1"){
event.value = "\$200";
}
if(this.getField("Test Number").value == "2"){
event.value = "\$150";
}
if(this.getField("Test Number").value > "2"){
event.value = "\$100";
}
if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

I tried to do this but it didn't work. So now i need to figure out another way or some other code to make this work.

if(this.getField("Test Number").value == "IN1"){
event.value = "\$75";
}
if(this.getField("Test Number").value == "IN2"){
event.value = "\$75";
}
if(this.getField("Test Number").value > "IN2"){
event.value = "\$75";
}
if(this.getField("Test Number").value == "1"){
event.value = "\$200";
}
if(this.getField("Test Number").value == "2"){
event.value = "\$150";
}
if(this.getField("Test Number").value > "2"){
event.value = "\$100";
}
if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

HOWEVER, I NEED TO ADD TO THIS OR FIGURE OUT ANOTHER WAY TO DO THIS WITH THE ADDITION OF THE FOLLOWING. I'M JUST NOT SURE WHAT OR HOW TO MAKE THIS HAPPEN. I TRIED DIFFERENT THINGS BUT NOTHING SEEMED TO WORK.

I THOUGHT OF CREATING ANOTHER CHECK BOX FIELD THAT WILL TRIGGER THIS BUT THAT'S JUST TOO MUCH FOR THE PEOPLE USING THESE FORMS TO UNDERSTAND AND DO.

BASIC INFORMTION: ANY PUBLIC TEST

TEST #1 ENTERED INTO "TEST#" FIELD= A TOTAL COST OF \$200 (IS CALCULATED IN "TEST COST2" READ ONLY HIDDEN NOT PRINTABLE FIELD. + A RENTAL FEE IF A RENTAL IS USED AS EXPLAINED BELOW, WH

TEST #2 ENTERED INTO "TEST#" FIELD= A TOTAL COST OF \$150 (IS CALCULATED IN "TEST COST2" READ ONLY HIDDEN NOT PRINTABLE FIELD. + A RENTAL FEE IF A RENTAL IS USED AS EXPLAINED BELOW

TEST #3 OR MORE ENTERED INTO "TEST#" FIELD= A TOTAL COST OF \$100 (IS CALCULATED IN "TEST COST2" READ ONLY HIDDEN NOT PRINTABLE FIELD. + A RENTAL FEE IF A RENTAL IS USED AS EXPLAINED BELOW

BASIC INFORMTION: ANY SCHOOL BUS OR INDOT TEST (THIS IS THE PART I CAN'T UNDERSTAND HOW TO ADD OR DO)

IF THE TEST IS A SCHOOL BUS, IT CAN ONLY BE A FEE OF \$100.
IF THE TEST IS AN INDOT TEST IT CAN ONLY BE A FEE OF \$75 + RENTAL FEE IF RENTING.

IF THE RENTAL IS NOT USED, THE USER SELECTS THE RAIDO BUTTON OF NO, AND THE BLANK FIELD CALLED "COST OVERLAY" IS THEN SHOWN BUT IS A BLANK READ ONLY WHITE BACKGROUND BOX TO SHOW THE SPACE BEING EMPTY.  (WOULD LIKE IT TO JUST SAY "NONE", BUT I CAN'T FIGURE THAT OUT EITHER. HOWEVER, THAT'S NOT IMPORTANT)

IF THEY SELECT YES FOR RENTAL, THEN THEY ENTER THIER OWN RENTAL FEE AND THEN THE TEST COST FIELD CALLS THE "TEST COST2" FIELD AND THEY JUST SUM UP THOSE 2 VALUES TO DISPLAY THE CORRECT COST IN THE "TEST COST" FIELD.

DOES ANYONE HAVE ANY IDEA'S ON HOW THIS CAN BE ACCOMPLISHED?

TOPICS
JavaScript , PDF forms

Views

256

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

## 2 Correct answers

Community Expert , Jun 27, 2024 Jun 27, 2024

First your script that you say is working actually doesn't work correctly, for last two conditions:
if(this.getField("Test Number").value > "2"){
event.value = "\$100";
}
if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

You can't use comparison operators on strings, you need to remove quotes from numbers.
Correct way:
if(this.getField("Test Number").value > 2){
event.value = "\$100";

(TIP: if you format the field as number and select currency sign you can input; event.value = 100, it wi

...

Community Expert , Jun 28, 2024 Jun 28, 2024

Acrobat JavaScript may convert "1" to a number when comparing, but it is not reliable to depend on this implicit conversion.
For mathematical comparisons, you should explicitly convert the string to a number.

Also, not using 'else if' can result in multiple conditions being true simultaneously, leading to unexpected behavior.

I mean that script will not work properly is because your last condition:

if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

Which now you changed to < "1" i

...

7 Replies 7
Jun 27, 2024 Jun 27, 2024

Copy link to clipboard

Copied

Hi there

Hope you are doing well and thanks for reaching out.

The workflow you are trying to achieve is might be possible using JavaScript. For more information please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/

Hope it will help

Regards
Amal

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 Expert ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

This canned reply is not very useful. They know they need to use a script. In fact, they are already doing that. They are asking for help with that script, and these links don't help with that.

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 Expert ,
Jun 27, 2024 Jun 27, 2024

Copy link to clipboard

Copied

First your script that you say is working actually doesn't work correctly, for last two conditions:
if(this.getField("Test Number").value > "2"){
event.value = "\$100";
}
if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

You can't use comparison operators on strings, you need to remove quotes from numbers.
Correct way:
if(this.getField("Test Number").value > 2){
event.value = "\$100";

(TIP: if you format the field as number and select currency sign you can input; event.value = 100, it will automatically add currency sign in case you use it in another calculation since "\$100" is a string)

even if you fix strings issue, your conditions are not set correctly, you need to use 'else if' and not just 'if' conditions,
in your current script (the one you say works) the last condition will overwrite second and third condition, because if you input 2 or greater last condition will always be true and set value to 0.

In your second script, you need to use 'else if' and you can't use comparison operator to compare strings same as in your first script, especcialy this one "IN2".

I would suggest you to post your file it will be easier when we can see what is actually going on in the file.

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 Beginner ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

UNDERSTOOD FOR MOST OF THAT. I'LL GET MY FILE UPLOADED SOON AS I CAN SO YOU CAN SEE EXACTLY WHAT I'M WORKING WITH. THANK YOU SO MUCH FOR THE INFORMATION.

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 Beginner ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

OK I HAVE BEEN GIVEN THE AUTHORITY TO SHOW THIS HERE TO GET ASSISTANCE BY THE STATE. I'M NOT AUTHORIZED TO RELEASE THE ACTUAL DOCUMENT, BUT I WAS ALLOWED TO RELEASE WHAT PORTIONS I AM ABLE TO IN ORDER TO GET HELP. SO I TRANSFERED IT ALL TO A BASIC BLANK FORM FOR THIS PURPOSE.

NESA, WHAT I DON'T UNDERSTAND IS HOW YOU SAY THAT MY SCRIPT DOENS'T WORK OR INFACT WILL NOT WORK PROPERLY. HOWEVER, IF YOU DO SEE THIS, YOU CAN TEST THE FORM OUT AND SEE THAT IT DOES WORK PROPERLY. I'M NOT SURE WHERE YOU SEE THE ISSUE SAYING IT DOENS'T WORK PROPERLY. CAN YOU EXPLAIN WHERE IT DOENS'T WORK OR WON'T WORK. I HAVE TRIED DOZENS OF TIMES TO BREAK WHAT I HAVE HERE WORKING AND HAVE YET TO COME TO A BREAK. THE ONLY THING THAT DOES NOT WORK PROPERLY BASED ON THAT FIELD IS THE ENTRY OF A "0" FOR PRINTING PURPOSES. WHICH I CAN ACTUALLY REMOVE THAT, IT'S NOT NEEDED, I JUST WANTED TO TRY IT OUT FOR THE STATE REPS. OTHER THAN THAT, I CAN'T SEE WHY YOUR STATING IT'S NOT WORKING RIGHT.

I'M GAME FOR TRYING ANYTHING TO ACCOMPLISH MY GOALS. I HAVE A TEMP SOLUTION AND THAT IS TO JUST MAKE OTHER FILES AND CHANGE THE VALUES IN THE CODING, JUST TO REFLECT SCHOOL BUS AND INDOT TESTS. BUT I'D LIKE TO TRY AND NOT DO THAT, AND MAKE THEM ALL UNIVERSAL. ANYWHO, HERE IS WHAT I HAVE CREATED THAT WE HAVE BEEN USING FOR 15+ YEARS NOW. HOWEVER, WITH THE NEW STUFF I POSTED ABOUT ABOVE I HAVE ALREADY STATED.

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 Expert ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

Acrobat JavaScript may convert "1" to a number when comparing, but it is not reliable to depend on this implicit conversion.
For mathematical comparisons, you should explicitly convert the string to a number.

Also, not using 'else if' can result in multiple conditions being true simultaneously, leading to unexpected behavior.

I mean that script will not work properly is because your last condition:

if(this.getField("Test Number").value > "1"){
event.value = "\$0";
}

Which now you changed to < "1" instead of > "1" which would lead to show 0 if number is 2 or greater.

You can use script like this:

``````var testNumber = Number(this.getField("Test Number").valueAsString);

if (testNumber === 1) {
event.value = "\$200";}
else if (testNumber === 2) {
event.value = "\$150";}
else if (testNumber > 2) {
event.value = "\$100";}
else if (testNumber < 1) {
event.value = "\$0";}``````

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 Beginner ,
Jun 28, 2024 Jun 28, 2024

Copy link to clipboard

Copied

LATEST

I SEE YOUR POINT NOW. I'LL GIVE THIS A TRY AND SEE WHAT I HAPPENS. ANY IDEA'S ON THE OTHER TASKS I'M TRYING TO ACCOMPLISH HERE?

THANK YOU VERY MUCH.