Show commas in numeric value in the service catalog

Marcel H_
Tera Guru

I have a service catalog item that has a field used to enter a number, which is then mapped to a Price field on the task form. I've seen a few posts on enforcing characters and requiring users to enter a dollar amount in the correct format, which is fine, but what I'm being asked to do is the following:

Validate that no letters or characters not normally associated with US currency are allowed (0-9 $ , .)

Format the number as it is typed to automatically insert commas at the correct intervals (eg. 1,000     10,000     100,000 etc.)

Automatic insertion of the comma is to help prevent user error when they type something like 25000 quickly but meant to type 250000. Trailing zeros can make it difficult to spot errors, apparently

Not sure if there is really a way to do this. Would be nice to have an Integer type field for the service catalog.

1 ACCEPTED SOLUTION

Hi Marcel,



You can try below script :


function onChange(control, oldValue, newValue, isLoading, isTemplate) {



  if (isLoading || newValue === '') {


  return;


  }


  var result = formatNumber(newValue);


  if(result == false){


        alert('Only numeric values allowed');


        return;



  }


    // To avoid recusrion


  if(result != newValue){


      // Change u_field_name to the field on which this onChange script is applied


      g_form.setValue('u_field_name', formatNumber(newValue));



  }


}


function formatNumber(x) {


  // Regular expression allowing only numeric values


  var reg = /^[\d,]+$/;


  if(!x.match(reg))


      return false;


  else


      return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");   // Insert commas at correct position


  }




Let me know if this works fine


View solution in original post

14 REPLIES 14

If we need comma in both numerical and decimal values what the script??

Daniel Oderbolz
Kilo Sage

IMHO this belongs in the area of "do not implement yourself". Ideally, your formatter would respect the locale settings of the instance, let alone the user.

So SNOW should provide such a function...

 

Best
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

Actually, this function is even built into Javascript:

https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript

So your code would simplyfy to x.toLocaleString("en-US");

Unfortunately, the Rhino Engine used in SNOW (at least currently in Kingston) does not support the locale to be passed.

 


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

So, the best I came up with (still ignoring locale) is

 

function formatNumber(strNumber) 
{
	var strGroupSep = "'";
	var strDecimalSep = "."
	var result;

	if(/^[0-9]+(\.([0-9]+)?)?$/.test(strNumber)) 
	{
		// We need to treat the fraction separate
		var intDecSepPos = strNumber.indexOf('.', 0);

		 if (intDecSepPos > 0) 
		 {
			// a. Let integer be the substring of n from position 0, inclusive, to position decimalSepIndex, exclusive.
			strInt = strNumber.substring(0, intDecSepPos);
			// b. Let fraction be the substring of n from position decimalSepIndex, exclusive, to the end of n.
			strFraction = strNumber.substring(intDecSepPos + 1, strNumber.length);
			console.log(strFraction);
		}
		else
		{
			strInt = strNumber
			strFraction = "";
		}

		var strFormattedInt = strInt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, strGroupSep );

		if (strFraction)
		{
			result = strFormattedInt + strDecimalSep + strFraction;
		}
		else
		{
			result = strFormattedInt;
		}
		
	}
	else
	{
		result = strNumber;
	}

	return result;
}

If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

Phani22
Kilo Contributor

I have a client script using the below command to insert comma

return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");     // Insert commas at correct position 

Works fine first time

Says first time input field 10000

output 10,000

But when user adds value a, it the end code breaks

say user input 100000

output becomes 10,0,000

pls help