Regex for Numbers which automatically add comma and decimal

Community Alums
Not applicable

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.

 

var result = formatNumber(newValue);
   if(result == false){
    alert('Only numeric values allowed');
    g_form.setValue('u_opex_cost_yearly_in_usd', '');
    return;
   }

   if(result != newValue){
    g_form.setValue('u_opex_cost_yearly_in_usd', formatNumber(newValue));
   }
   
}
function formatNumber(number){
    var reg = /^[\d,]+$/;
    if(!number.match(reg))
    return false;
    else
    return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   }
 
 
Regards,
Prudhvi

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

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

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

@Ankur Bawiskar - Not sure what the issue but it's still giving same error even if tried with different regex format.

 var formatted = formatNumber(newValue);
   if(formatted  == false){
    alert('Only numeric values allowed');
    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+)?$/;
    //var reg = /^[0-9].*$/;
    var reg = /^[\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];
}