Skip to main content
Zeital
Participant
July 17, 2019
Answered

Custom calculation

  • July 17, 2019
  • 2 replies
  • 641 views

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;

This topic has been closed for replies.
Correct answer try67

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;

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 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;

Zeital
ZeitalAuthor
Participant
July 17, 2019

Cheers, that worked a treat!

Bernd Alheit
Community Expert
Community Expert
July 17, 2019

The value of a unchecked checkbox is "Off".