Skip to main content
Known Participant
March 13, 2023
Answered

How do I clear the last input if total exceeds the value of another field ?

  • March 13, 2023
  • 2 replies
  • 1249 views

I have five input fields (amount.1 through amount.5) that sum up in a Total field. In the Total field, I have a script (see below) in Validate that compares to another field (Adjustment amount) and alerts the user if  Total exceeds Adjustment amount. The alert works fine, but I would like for it to clear or zero out the last field that was input that triggered the alert. 

 

var TAA = this.getField("txtAdjustmentAmt").value;

if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
	if (event.value > TAA){
		app.alert("Total to apply cannot exceed the Adjustment amount")
		event.rc = false;
	} 
}
This topic has been closed for replies.
Correct answer try67

You can't use the value of another field for the total calculation, because that won't update until the new value is validated. Instead, you need to calculate the total in your code (making sure to use the new value of the validated field, instead of the old one). Something like this:

 

function validateAmount() {
	var TTL = 0;
	for (var i=1; i<=5; i++) {
		var fname = "amount."+i;
		if (event.target.name==fname) TTL+=Number(event.value);
		else TTL+=Number(this.getField(fname).valueAsString);
	}

	var TAA = this.getField("txtAdjustmentAmt").value;
	if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
		if (TTL > TAA){
			app.alert("Total to apply cannot exceed the Adjustment amount")
			event.rc = false;
		} 
	}
}

 

Then call validateAmount() from the Validation event of each of the amount.x fields.

However, note that this will not work if you change the value of txtAdjustmentAmt once the amount fields have been filled in. I would recommend resetting those fields when that happens, to make sure the values are valid.

2 replies

try67
Community Expert
Community Expert
March 14, 2023

You should move the code to the Validation script of each one of the amount fields. Then you would be able to reject the latest value entered, if it exceeds the allowed total.

CeltexanAuthor
Known Participant
March 14, 2023

Hello Try67,

I tried as you suggested, but the alert message does not appear until a calculation (step) after the Total has exceeded the Adjustment amount. Below is the code used in each of the Amount fields:

var TAA = this.getField("txtAdjustmentAmt").value;
var TTL = this.getField("txtTtlToApply").value;

if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
	if (TTL > TAA){
		app.alert("Total to apply cannot exceed the Adjustment amount")
		event.rc = false;
	} 
}
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 14, 2023

You can't use the value of another field for the total calculation, because that won't update until the new value is validated. Instead, you need to calculate the total in your code (making sure to use the new value of the validated field, instead of the old one). Something like this:

 

function validateAmount() {
	var TTL = 0;
	for (var i=1; i<=5; i++) {
		var fname = "amount."+i;
		if (event.target.name==fname) TTL+=Number(event.value);
		else TTL+=Number(this.getField(fname).valueAsString);
	}

	var TAA = this.getField("txtAdjustmentAmt").value;
	if (TAA > 0) { //If TAA returns with a negative number, then the following will not occur
		if (TTL > TAA){
			app.alert("Total to apply cannot exceed the Adjustment amount")
			event.rc = false;
		} 
	}
}

 

Then call validateAmount() from the Validation event of each of the amount.x fields.

However, note that this will not work if you change the value of txtAdjustmentAmt once the amount fields have been filled in. I would recommend resetting those fields when that happens, to make sure the values are valid.

Thom Parker
Community Expert
Community Expert
March 13, 2023

In a custom calculation,  the field that triggered the calculation is provided in the event object. 

in "event.source".  But of course this could be any field on the form. 

Here's the reference entry:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#event-properties

 

One solution is to capture and save the value in the calculation script so it can be used later in the validation script. 

Another solution is to put all the code into the calculation script.  

 

For this to work the calculation will need to be setup to filter out any sources that are not from the "amount" fields. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often