How to add a $ symbol as prefix in a variable ?

Beast
Tera Contributor

I have a variable named amount in my record producer. After adding the value to the variable i want to add a $ symbol infront of the value. 

EG : If I enter 2000 , I want $2000.

I tried onChange client script but it is not worked as expected.

 

Please help

Thanks

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

Does this help?

function FormatPrice(varName, strValue) {
   //varName is the variable name
   //strValue is the entered value of the Single Line Text variable
 
   var xNum = Number(strValue.replace(/[^0-9.]/g, "")).toFixed(2);
   var xStr = xNum.toString();
   xStr = '$' + xStr.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   g_form.setValue(varName, xStr);
   return;
}

This function is then called on the OnChange catalog client script for the cost variable

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   FormatPrice('cost', newValue);
   return;
}

If my answer helped you in any way, please then mark it as helpful.

Mark


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

7 REPLIES 7

SumanthDosapati
Mega Sage
Mega Sage

Hi,

What type of field is that amount?

Can you also share the script you already tried

 

Regards,
Sumanth

It is string filed. 

This is my code.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
 var amount = g_form.getValue('amount');
 g_form.clearValue('amount');
g_form.setValue('amount','$'+amount);

}

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
try 
{
var str = '$'+newValue;
g_form.setValue('your_cost_field',str);
}
catch(e)
{}
}