Skip to main content
Participant
January 10, 2019
Answered

I need to subtract a constant

  • January 10, 2019
  • 1 reply
  • 483 views

I have a question about doing subtraction in Forms. I need to subtract a constant from another field but return a zero if the first field is blank. This is for a mileage form, and not all fields are filled in. I only want to subtract a constant if there is a value in the "Miles Traveled" field. I have tried:

event.value = getField("Miles Trav 1").value - 30.0;

This works except when the "Miles Trav" field has a value, but if there is no value, then it returns a minus number. I need an IF THEN where it only subtracts if there is a value entered. I'm a real novice at this and I'm stuck.

This topic has been closed for replies.
Correct answer try67

Use this:

var v = this.getField("Miles Trav 1").valueAsString;

if (v=="") event.value = "";

else event.value = Number(v) - 30.0;

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 10, 2019

Use this:

var v = this.getField("Miles Trav 1").valueAsString;

if (v=="") event.value = "";

else event.value = Number(v) - 30.0;

Participant
January 10, 2019

That works perfectly, thanks!