Add a minus sign to a field, when a certain condition is met

Emmat
Tera Contributor

I have a catalog form that asks the client an initial question(request_type). If the answer to this question is 'Remove' I would like to add a minus sign to 2 fields that the client must complete (monthly_charge_out_amount and budget_transfer_amount).

These 2 fields are Single Line Text fields. Currently the form is being submitted and people are forgetting to add a '-' sign so I would like to automatically add one. The following code is what i have so far but its not working:

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

// Check if request_type is "Remove."
if (g_form.getValue('request_type') === 'Remove') {
// Get the values of the fields.
var monthlyChargeOutAmount = g_form.getValue('monthly_charge_out_amount');
var budgetTransferAmount = g_form.getValue('budget_transfer_amount');

// Add a "-" sign to the values if they don't already start with "-".
if (monthlyChargeOutAmount !== "" && monthlyChargeOutAmount.charAt(0) !== '-') {
monthlyChargeOutAmount = '-' + monthlyChargeOutAmount;
g_form.setValue('monthly_charge_out_amount', monthlyChargeOutAmount);
}

if (budgetTransferAmount !== "" && budgetTransferAmount.charAt(0) !== '-') {
budgetTransferAmount = '-' + budgetTransferAmount;
g_form.setValue('budget_transfer_amount', budgetTransferAmount);
}
}
}

If anyone has any ideas or suggestions i would greatly appreciate it.
Thank-you

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Add some alert lines to your script to see how far it is getting, the result of each if condition,... If charAt is not working for you, you might need to add a .toString() before it, or try indexOf:

if (monthlyChargeOutAmount != "" && monthlyChargeOutAmount.toString().indexOf('-') != 0) {