PDA

View Full Version : % and $ rule


pbrown
September 20th, 2011, 11:33 AM
I have a template where a customer can enter an amount off for a coupon. It can be a dollar amount or a percent. So I am trying to create a rule that will superscript the % if a percent amount is entered or superscript the $ and cents if a dollar amount is entered. I need one rule that does both and I am struggling creating it. Does anybody have such a rule? Thanks.

esmith
September 20th, 2011, 01:23 PM
Can you add a sample PDF showing the various formats you want to handle along with a data file to show how your data will be supplied?

If your field will contain either "1.99", ".99", or "20%", you could do something like:
var amount = Field(YOUR FIELD");
// convert cent-only values to ## format
if (Left(amount,1) == ".") amount = Right(amount,2) + "";
// convert dollar values to $#.## format
else if (amount.indexOf("%") == -1) amount = "$" + amount;
// add superscripting for symbols
amount = amount.replace("$","<superscript>$</superscript>").replace("","<superscript></superscript>").replace("%","<superscript>%</superscript>");
return amount;
Of course, this may not work if other values can be input (i.e. "$1.99", "$.99", "0.99", etc).

pbrown
September 20th, 2011, 01:44 PM
Thanks for the quick reply.
The rule works except for when someone adds their own $ sign. The rule as is will then have 2 $ signs. So is it possible to tweak this rule to remove the one entered?

esmith
September 21st, 2011, 05:59 AM
Sure. Just add what's in red:
var amount = Field(YOUR FIELD").replace("$","");;
// convert cent-only values to ## format
if (Left(amount,1) == ".") amount = Right(amount,2) + "";
// convert dollar values to $#.## format
else if (amount.indexOf("%") == -1) amount = "$" + amount;
// add superscripting for symbols
amount = amount.replace("$","<superscript>$</superscript>").replace("","<superscript></superscript>").replace("%","<superscript>%</superscript>");
return amount;
Although you'll still have a problem if the end user is able to place a leading zero for a cents-only value (i.e. "0.49"). For this reason, when we create a portal, we usually limit the characters that can be typed in the field and then "clean up" the result before forwarding to FP.