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

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

Explorer ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

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;
	} 
}
TOPICS
How to , JavaScript , PDF forms

Views

591

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

correct answers 1 Correct answer

Community Expert , Mar 14, 2023 Mar 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);
	
...

Votes

Translate

Translate
Community Expert ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

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-propertie...

 

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

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.

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
Explorer ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

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;
	} 
}

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 ,
Mar 14, 2023 Mar 14, 2023

Copy link to clipboard

Copied

LATEST

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.

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