Regex for Numbers which automatically add comma and decimal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:33 AM
Hello,
I have a custom string field on record producer, which should allow only integers with decimals and add commas I have have added below script in catalog script but it's not allowing decimals and user is not allowed to enter only 0 value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:45 AM
@Community Alums
try this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var formatted = formatNumber(newValue);
if (formatted === false) {
alert('Only numeric values allowed (integers or decimals).');
g_form.setValue('u_opex_cost_yearly_in_usd', '');
return;
}
if (formatted !== newValue) {
g_form.setValue('u_opex_cost_yearly_in_usd', formatted);
}
}
function formatNumber(number) {
// Remove existing commas
number = number.replace(/,/g, '');
// Allow numbers like "0", "0.5", "123", "123.45"
var reg = /^(0|[1-9]\d*)(\.\d+)?$/;
if (!reg.test(number)) {
return false;
}
// Split integer and decimal parts
var parts = number.split('.');
// Add commas to integer part
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.length > 1 ? parts[0] + '.' + parts[1] : parts[0];
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:57 AM
Hi @Ankur Bawiskar - Thanks for the response, all the scenarios are working fine but still when user enter a single "0" it's giving error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:05 AM
@Community Alums
it should work fine ideally and allow 0
use clearValue()
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var formatted = formatNumber(newValue);
if (formatted === false) {
alert('Only numeric values allowed (integers or decimals).');
g_form.clearValue('u_opex_cost_yearly_in_usd');
}
if (formatted !== newValue) {
g_form.setValue('u_opex_cost_yearly_in_usd', formatted);
}
}
function formatNumber(number) {
// Remove existing commas
number = number.replace(/,/g, '');
// Allow numbers like "0", "0.5", "123", "123.45"
var reg = /^(0|[1-9]\d*)(\.\d+)?$/;
if (!reg.test(number)) {
return false;
}
// Split integer and decimal parts
var parts = number.split('.');
// Add commas to integer part
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.length > 1 ? parts[0] + '.' + parts[1] : parts[0];
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:35 AM
@Ankur Bawiskar - Not sure what the issue but it's still giving same error even if tried with different regex format.