Skip to main content
ZombieLuna
Inspiring
March 9, 2023
Question

Calculate Value of Fields if Checkbox is Triggered

  • March 9, 2023
  • 2 replies
  • 841 views

Hello All,

I am probably making this more complicated than it needs to be, but I want to test the totals of 2 fields in the form that I have created.

I have a hidden field ("Split Test") that should add together the 2 fields ("Amount To Be Paid On First Card" & "Amount To Be Paid On Second Card") if the checkbox marking the payment as multiple is checked ("MP Yes").  This checkbox is not a radio button, there is a checkbox for Yes and a checkbox for No.  No is the default selection.

There is an app.alert within the code that needs to inform the user that the amounts for the cards cannot equal less than $99 or more than $1749.  But, if the multiple payment box hasn't been checked, this should not be tested at all.

I have tried several different variations of code that I have found, inclusive of adjusting the codes that I already have in place, but to no avail.

Any assistance that can be provided would be appreciated.

 

I did find a way to trigger it so that it would add together and clear the fields if the amount was over or under the max or min.  However, what I have found is that the box checks it whether it is a multiple payment or not, which is confusing if you don't know what is happening (as the second payment fields are all hidden unless the multiple payment Yes checkbox has been checked.

This is what I had that *did* work, sort of:

[This goes in the custom calculation field of Split Test]
var a = Number(this.getField("Amount To Be Paid On First Card").valueAsString);
var b = Number(this.getField("Amount To Be Paid On Second Card").valueAsString);
event.value = a+b;
if (event.value <99 || event.value >1749) 
	{event.value = "";
     app.alert("Multiple credit card payments cannot equal less than $99 or more than $1,749");
}

[This goes in the custom calculation field of Amount to be Paid on First Card and Amount to be Paid on Second Card]
var v = this.getField("Split Test").valueAsString;
if (v==0) event.value=""

 

Please help.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
March 9, 2023

Try this:

 

// custom validation script for "Amount To Be Paid On First Card"
var a = Number(event.value);
var b = Number(this.getField("Amount To Be Paid On Second Card").valueAsString);
var total = a+b;
if (total <99 || total >1749) {
	event.rc = false;
    app.alert("Multiple credit card payments cannot equal less than $99 or more than $1,749");
}

// custom validation script for "Amount To Be Paid On Second Card"
var a = Number(this.getField("Amount To Be Paid On First Card").valueAsString);
var b = Number(event.value);
var total = a+b;
if (total <99 || total >1749) {
	event.rc = false;
    app.alert("Multiple credit card payments cannot equal less than $99 or more than $1,749");
}

 

 

ZombieLuna
Inspiring
March 10, 2023

Okay!  It works!  Ish.

 

So, this code already exists in the Amount To Be Paid On First Card:

if (event.value){
if (this.getField("Other Payment").valueAsString != "Off"){
    if (event.value <99 || event.value >7500) {
    event.value = "";
		app.alert("Other payments cannot be less than $99 or more than $7500");
	}
} else if (event.value <99 || event.value >1749) {
    event.value = "";
	    app.alert("Credit card payments cannot be less than $99 or more than $1,749");
}
}

When I apply what you have provided, it breaks the above code in the First Card Field.

It works perfectly in the Second Card Field.

So, I took away everything that was an attempt to use Split Test as a trigger and deleted that field.  Removed the codes from First Card that weren't already in place to make the document work the way that it already was.  And I applied the code that you gave me, but only to the Second Card.

And it works!

There is behavior that I would like to try to make happen...but I'm gonna take the win and not break anything!  Lol

Thank you so much for your help!

try67
Community Expert
Community Expert
March 9, 2023

It's not a good idea to do it like that, as the alert message will keep showing up each time you edit any field in the file. You need to either clear the Amount fields in your code, or move it to the Validation script of those fields, rejecting the value entered into them if it's too small or large. That way the alert will only appear once, not all the time.

ZombieLuna
Inspiring
March 9, 2023

I might need more guidance.

 

I moved all to the validation fields, I moved only the code on the Amounts fields, and I moved only the code on the Split test and they either didn't work at all or showed the app alert no matter what.