Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Regex for Numbers which automatically add comma and decimal

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

@Community Alums 

try to debug line by line where it's failing

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

@Community Alums 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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