Custom calculation

New Here ,
Jul 16, 2019 Jul 16, 2019

Copy link to clipboard

Copied

Hi,

I have a script running but I am running into an issue between using the text fields and checkbox.

Text1 has a default value of 15, this is multiplied by Text2 which the user can set (say for this example 6) + the value set to the checkbox (say 50).

When the Text2 and the checkbox are empty the Total field is displaying "0Off" and if I fill in Text2 with 6 (15*6=90) it will show 90Off and with the checkbox finally checked it then displays the total of 140

or if I change the Total field format to "number" I get an error message whenever one of the fields changes "The value entered does not match the format of the field [ Total ]" and does not 0 back out when the Text2 or checkbox are emptied again.

Here is the code I am putting into the Total field for reference.

event.value = ( this.getField("Text1").value * this.getField("Text2").value ) + this.getField("checkbox").value;

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

157

Likes

translate

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

Most Valuable Participant , Jul 17, 2019 Jul 17, 2019
In addition to Bernd's correct remark, you also need to convert the values of the fields to numbers.Try this code:var v1 = Number(this.getField("Text1").valueAsString)var v2 = Number(this.getField("Text2").valueAsString)var v3 = (this.getField("checkbox").valueAsString=="Off") ? 0 : Number(this.getField("checkbox").valueAsString);event.value = ( v1 * v2 ) + v3;

Likes

translate

Translate

Translate
Adobe Community Professional ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

The value of a unchecked checkbox is "Off".

Likes

translate

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
Most Valuable Participant ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

In addition to Bernd's correct remark, you also need to convert the values of the fields to numbers.

Try this code:

var v1 = Number(this.getField("Text1").valueAsString)

var v2 = Number(this.getField("Text2").valueAsString)

var v3 = (this.getField("checkbox").valueAsString=="Off") ? 0 : Number(this.getField("checkbox").valueAsString);

event.value = ( v1 * v2 ) + v3;

Likes

translate

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
New Here ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

Cheers, that worked a treat!

Likes

translate

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