These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

If text field empty, hide. If text present, run calculation.

407chelsie
Registered: Dec 8 2008
Posts: 68
Answered

I am trying to calculate a discount, but the discount is only available if a text field is filled (Sponsor Membership No). After reading lots of forum posts, I tried this:

var sub = +getField("Subtotal").value;
var sps = +getField("Sponsor Membership No");
 
if (sps == Null) {
event.value = "";
} else {
event.value = sub * 0.2;
}

I also need this to be a negative number, but I can't even get it working as a positive. I hope someone can show me where I went wrong. Thanks.

My Product Information:
Acrobat Pro 8.1.2, Macintosh
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Try this:

var sub = +getField("Subtotal").value;var sps = getField("Sponsor Membership No").valueAsString; if (!sps) {event.value = "";} else {event.value = -sub * 0.2;}

But this is a bit more straighforward:

var sub = +getField("Subtotal").value;var sps = getField("Sponsor Membership No").valueAsString; if (sps) {event.value = -sub * 0.2;} else {event.value = "";}

And better yet:
(function () {var sub = +getField("Subtotal").value;var sps = getField("Sponsor Membership No").valueAsString;event.value = sps ? (-sub * 2) : "";})();

You forgot to get the value of the field. Also, a field value will never be equal to the special JavaScript value of null (not Null), so it's misleading to test for it.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
Thank you, George!