
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2017 01:21 PM
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.
Solved! Go to Solution.
- 7,018 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2017 10:24 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2022 04:36 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2018 12:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2018 01:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2018 02:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2021 01:35 AM
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