k_p_
Giga Contributor

This script is for users who want a currency-like variable in service catalog; the script enforces a variable in service catalog (i'm using single line text variable) to maintain a correct currency format. Copy paste the following script in an onSubmit - Catalog Client Script.

[NOTE: currencySign & fieldName are the ONLY 2 variables in the script you need to configure.]

FOOLPROOF

Detects & notifies invalid characters in the input field.

find_real_file.png

find_real_file.png

 

Detects & notifies too many numbers after decimal.

find_real_file.png

find_real_file.png

 

Detects & removes leading zeroes.

find_real_file.png

 

Detects & re-formats incorrectly formatted input.

find_real_file.png

On successful submission, input will always be in this format.

find_real_file.png

THE SCRIPT

 

//configurable variables
var currencySign = '$';
var fieldName = '<catalog_variable_name>';

//DO NOT CHANGE FOLLOWING CODE:
var rawValue = g_form.getValue(fieldName);
var fieldLabel = g_form.getLabelOf(fieldName);
var signValue, amount, element, formattedValue, value, decimalValue, decimal, outputString;

//check for invalid characters
if(!checkValidChars(rawValue)){
return false;
}

//check for valid decimal value (.00)
if(!checkDecimalValue(decimalValue)){
return false;
}

//functions
function getAmount(tempElement){
var tempAmount;
for(i=0; i < tempElement.length; i++){
if(i==0){
tempAmount = tempElement[i];
}
else{
tempAmount += tempElement[i];
}
}
return tempAmount;
}

function checkValidChars(tempRawValue){
if(tempRawValue.length==0){
getNumAmount();
formatValidInput();
return true;
}
else if(containsSingleValue(tempRawValue, currencySign)){
return checkValidChars(tempRawValue.replace(currencySign, ''));
}
else if (containsSingleValue(tempRawValue, '.')){
return checkValidChars(tempRawValue.replace('.', ''));
}
else if (containsSingleValue(tempRawValue, ',')){
return checkValidChars(tempRawValue.replace(',', ''));
}
else if (containsSingleValue(tempRawValue, '0')){
return checkValidChars(tempRawValue.replace('0', ''));
}
else if (containsSingleValue(tempRawValue, '1')){
return checkValidChars(tempRawValue.replace('1', ''));
}
else if (containsSingleValue(tempRawValue, '2')){
return checkValidChars(tempRawValue.replace('2', ''));
}
else if (containsSingleValue(tempRawValue, '3')){
return checkValidChars(tempRawValue.replace('3', ''));
}
else if (containsSingleValue(tempRawValue, '4')){
return checkValidChars(tempRawValue.replace('4', ''));
}
else if (containsSingleValue(tempRawValue, '5')){
return checkValidChars(tempRawValue.replace('5', ''));
}
else if (containsSingleValue(tempRawValue, '6')){
return checkValidChars(tempRawValue.replace('6', ''));
}
else if (containsSingleValue(tempRawValue, '7')){
return checkValidChars(tempRawValue.replace('7', ''));
}
else if (containsSingleValue(tempRawValue, '8')){
return checkValidChars(tempRawValue.replace('8', ''));
}
else if (containsSingleValue(tempRawValue, '9')){
return checkValidChars(tempRawValue.replace('9', ''));
}
else{
swal('Invalid Currency Input', 'Field (' + fieldLabel + ') should only include '+ currencySign +', numbers, and decimal. Please remove the following invalid character(s): ' + tempRawValue, 'error');
return false;
}
}

function checkDecimalValue(tempDecimalValue){
var tempDecimalValueList = tempDecimalValue.split('.');
if(tempDecimalValueList.length > 1 || tempDecimalValueList[0].length > 2){
swal('Invalid Decimal Input', 'Field (' + fieldLabel + ') should only include 2 numbers after the decimal (e.g. $9.99).');
return false;
}
return true;
}

function removeLeadingZeros(tempAmount){
while(tempAmount.charAt(0) == 0){
if(tempAmount.length==1){
return tempAmount;
}
tempAmount = tempAmount.substring(1);
}
return tempAmount;
}

function getNumAmount(){
//break down to numerical amount
//check for decimal
if(containsSingleValue(rawValue, '.')){
decimal = rawValue.split('.');
if(decimal.length > 2){
decimalValue = decimal[1] + '.' + decimal[2];
}
else if(decimal[1].length == 0){
decimalValue = '00';
}
else if (decimal[1].length == 1){
decimalValue = decimal[1] + '0';
}
else{
decimalValue = decimal[1];
}
value = decimal[0];
}
else{
value = rawValue;
decimalValue = '00';
}

//check for sign
if(containsSingleValue(value, currencySign)){
signValue = value.split(currencySign);

if(containsSingleValue(signValue[1], ',')){
element = signValue[1].split(',');
amount = getAmount(element);
}
else{
amount = signValue[1];
}
}
else{
if(containsSingleValue(value, ',')){
element = value.split(',');
amount = getAmount(element);
}
else{
amount = value;
}
}
}

function formatValidInput(){
//reconstruct with proper formatting
var length = amount.length - 1;
var count = 0;
amount = removeLeadingZeros(amount);
for(i=length; i >= 0; i--){
if(i==length){
formattedValue = amount.charAt(i);
}
else if(count%3 == 0 && i!= length){
formattedValue = amount.charAt(i) + ',' + formattedValue;
}
else{
formattedValue = amount.charAt(i) + formattedValue;
}
count++;
}
outputString = currencySign + formattedValue + '.' + decimalValue;

//diaplsy output
g_form.setValue(fieldName, outputString);
}

//contains function that takes in a string & a single value like '.' ',' '1' '2' '$' etc.
function containsSingleValue(string, singleValue){
for(i=0;i<string.length;i++){
if(string[i]==singleValue){
return true;
}
}
return false;
}

Comments
k_p_
Giga Contributor

Updated the code to:

1. Make it compatible with IE.".includes()" method is not compatible with IE.

2. Stop submission & show error message when a value with multiple decimal is entered, "$123,456.12.12"

Uncle Rob
Kilo Patron

Tried to use this on a multi-row variable set and it causes the submission to hang.  (blank white screen forever)
Any ideas?

EDIT:  Punch me in my ragged empty eye socket.  OnSubmit isn't supported on multi-row variable sets.
Can this reliably be reconfigured for an onChange?

Marcel H_
Tera Guru

Is there an easy way to allow for negative amounts in the field as well?

Marcel H_
Tera Guru
I added “-“ as a valid character and that allows negative numbers and I’m able to pass the value to the record correctly, but the formatting in the catalog ends up displaying as $-,158.60 I’m not very familiar with the formatting code structure so hoping you might be able to point me in the right direction. Thanks!
Ravi KumarVerma
Tera Explorer

ravikumar.verma@hcltech.com

Version history
Last update:
‎05-18-2018 01:01 PM
Updated by: