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

How to calculate form field total to equal zero based on check box

Explorer ,
Jul 18, 2018 Jul 18, 2018

I was wondering if anyone could help me out, I've searched the forums and found an example of a check box being checked and a text field being populated with specified text but I'm unsure as to how to do it if I need a number to populate the field if there's already a calculation being done in it?

For example, I have a static number. ($5,000)..so meaning, it's not a form field, which needs to be added to the total of 11 text fields. So, under calculate in simple notation, I put in this information: (Field1 + Field2 + Field3 + Field4 + Field5 + Field6 + Field7 + Field8 + Field9 + Field10 + Field11) +5000 to get my "Total".

However, now, I have a checkbox further down, that when clicked, the "Total" from above needs to equal zero...but only if the checkbox is clicked "yes". Does that make sense?

TOPICS
PDF forms
3.8K
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 ,
Jul 19, 2018 Jul 19, 2018

In that case all you need to do is set the Mouse Up action of the check-box to execute this code:

if (event.target.value!="Off") this.resetForm(["Field1", "Field2", "Field3", ...]);

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 ,
Jul 18, 2018 Jul 18, 2018

Set the export value of the check-box to 1 and then multiply the sum by it, like this:

(Field1 + Field2 + Field3 + Field4 + Field5 + Field6 + Field7 + Field8 + Field9 + Field10 + Field11 + 5000) * CheckBox1

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 ,
Jul 18, 2018 Jul 18, 2018

Ah sorry, that would do the opposite of what you wanted... You would need to use a script to do it. Something like this:

if (this.getField("CheckBox1").valueAsString=="Off")

event.value = Number(this.getField("Field1").value) + Number(this.getField("Field2").value) + ... + 5000;

else event.value = 0;

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 ,
Jul 18, 2018 Jul 18, 2018

Amazing, thank you so much! I should have also said, that the fields 1-12 need to be empty once the total becomes "0"...right now, the numbers that were filled in remain, and the total just equals "0". What do I have to do to fix this issue?

Thank you so much for your help!

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 ,
Jul 18, 2018 Jul 18, 2018

Sorry, I don't follow what you mean. Isn't the total the sum of the fields? If you set them as zero, that would influence the total, which would in turn influence the fields. That's circular logic...

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 ,
Jul 19, 2018 Jul 19, 2018

So sorry, maybe I'm not explaining it well... or I put the script in the wrong place? I went to the "Total" field, and placed the script in the "custom calculation script" area. As soon as I click on the check box, the total goes to zero, like it's supposed to, but all the numbers in the fields 1-11 are still there.

I've attached a sample here...let me know if you're not able to access it.  This is the way it should work:

I filled in the form fields, with numbers...the total will add the numbers in the form field + the $5,000 number up at the top. Further down, there's an option to check a box, which should remove all the numbers in the fields 1-11, and place a "0" in the "Total" field.

Either that, or as soon as the check box is clicked, I need all the form fields to be cleared, and I need to be able to input the "0" in the Total text field myself manually. Let me know if that's clearer?

https://www.dropbox.com/s/iukuwr4zr03dpt5/testing2.pdf?dl=0

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 ,
Jul 19, 2018 Jul 19, 2018

In that case all you need to do is set the Mouse Up action of the check-box to execute this code:

if (event.target.value!="Off") this.resetForm(["Field1", "Field2", "Field3", ...]);

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 ,
Jul 19, 2018 Jul 19, 2018

Thank you so much! That's brilliant!

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 ,
Jul 31, 2018 Jul 31, 2018

Hi try67,

So, I did everything mentioned up top and everything was working. But then I added another check box further down on the page (one that is a "yes" "no", checkbox), which also has to do the same thing, meaning when the "no" is checked, the form fields gets reset, so the total is "0" again. I don't think I did it right, because the new check box I added is clearing and resetting the form, but the initial one is no longer not clearing the calculation, but leaving the 5000 total. Any idea what I could have done wrong?

Here's what I did (Dropbox - testing2.pdf )

Because I don't know much javascript, and how to combine calculations, I basically, copied the calculation I had done for the first checkbox and repasted it so they're on top of each other, and changed the second one to say "Checkbox2"):

if (this.getField("Check_box1").valueAsString=="Off")

event.value = Number(this.getField("Field1").value) + Number(this.getField("Field2").value) + Number(this.getField("Field3").value) + Number(this.getField("Field4").value) +

Number(this.getField("Field5").value) + Number(this.getField("Field6").value) +

Number(this.getField("Field7").value) + Number(this.getField("Field8").value) +

Number(this.getField("Field9").value) + Number(this.getField("Field10").value) +

Number(this.getField("Field11").value) + 5000;

else event.value = 0;

if (this.getField("Checkbox2").valueAsString=="Off")

event.value = Number(this.getField("Field1").value) + Number(this.getField("Field2").value) + Number(this.getField("Field3").value) + Number(this.getField("Field4").value) +

Number(this.getField("Field5").value) + Number(this.getField("Field6").value) +

Number(this.getField("Field7").value) + Number(this.getField("Field8").value) +

Number(this.getField("Field9").value) + Number(this.getField("Field10").value) +

Number(this.getField("Field11").value) + 5000;

else event.value = 0;

then under the "check box properties" for the actions, I did the action you had mentioned above for both check boxes that I'm referring to.

if (event.target.value!="Off") this.resetForm(["Field1", "Field2", "Field3","Field4", "Field5", "Field6","Field7", "Field8", "Field9","Field10", "Field11",]);

Not sure what I did wrong....I apologize for the stupid question...I did try really hard to figure it out myself

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 ,
Jul 31, 2018 Jul 31, 2018

You need to better explain how it should work in each one of the 4 possible situations:

- Check_box1 is ticked and Check_box2 is ticked

- CB1 is ticked and CB2 is not ticked

- CB1 is not ticked and CB2 is ticked

- CB1 is not ticked and CB2 is not ticked

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 ,
Jul 31, 2018 Jul 31, 2018

Actually there are more options than that:

1. CB1 is ticked and CB2 is Yes

2. CB1 is ticked and CB2 is No

3. CB1 is ticked and CB2 is not ticked

4. CB1 is not ticked and CB2 is Yes

5. CB1 is not ticked and CB2 is No

6. CB1 is not ticked and CB2 is not ticked

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 ,
Aug 20, 2018 Aug 20, 2018

Hey..sorry try67, had to think about the options....so I wrote below what needs to happen for each option. Is this clearer?

1. CB1 is ticked and CB2 is Yes- The total will go to "0".

2. CB1 is ticked and CB2 is No- The total will go to "0".

3. CB1 is ticked and CB2 is not ticked- The total will go to "0".

4. CB1 is not ticked and CB2 is Yes- Total will be equal the sum of all the form fields mentioned above.

5. CB1 is not ticked and CB2 is No- Total becomes "0".

6. CB1 is not ticked and CB2 is not ticked (so both left empty)- Total will be equal the sum of all the form fields mentioned above.

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 ,
Aug 20, 2018 Aug 20, 2018

Then you can use this script:

var cb1 = this.getField("CB1").valueAsString;

var cb2 = this.getField("CB2").valueAsString;

if (cb1=="Off" && cb2!="No") {

    event.value = Number(this.getField("Field1").value) + Number(this.getField("Field2").value) + Number(this.getField("Field3").value) + Number(this.getField("Field4").value) +

        Number(this.getField("Field5").value) + Number(this.getField("Field6").value) +

        Number(this.getField("Field7").value) + Number(this.getField("Field8").value) +

        Number(this.getField("Field9").value) + Number(this.getField("Field10").value) +

        Number(this.getField("Field11").value) + 5000;

} else event.value = 0;

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 ,
Aug 20, 2018 Aug 20, 2018

Amazing! So if I ever have to add an additional checkbox or checkboxes in this form (and checking the box doesn't affect the total at all), I'm assuming I'd have to add it to the script above to include var cb3 = this.getField ("CB3"). valueAsString; ?

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 ,
Aug 20, 2018 Aug 20, 2018

Yes, but you'll also need to include it in the if-conditions, of course.

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 ,
Aug 20, 2018 Aug 20, 2018

Right. Makes sense. How would the if- condition change if nothing actually happens if cb3 is 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 Expert ,
Aug 20, 2018 Aug 20, 2018
LATEST

Depends on how it should work... You will need to define a new "truth table", this time with 3 variables, which means many more possible combinations.

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