We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Phone number format for users

ahmed-24
Giga Guru

Hello Community,

I would like to change the format of the Mobile phone (mobile_phone) field in the User [sys_user] table.

Currently, the phone numbers are displayed in the following format:
Example: 1(449) 260-6439

However, I would like the numbers to be displayed and stored in a simple numeric format like:
Example: 06212348725

Requirement:

I want the Mobile phone number to:

  • Be displayed exactly as entered

  • Not automatically formatted with country code, parentheses, or dashes

  • Use a plain numeric format (example: 06212348725)

Althout i have disable the sys properties for us format but u still have the probleme  : 

 

 

ahmed24_0-1770732095288.png

Sys_properties : glide.ui.format_phone

ahmed24_2-1770732233610.png

 

Thank you for help 

 

1 ACCEPTED SOLUTION

ahmed-24
Giga Guru

Resolution

For the old "Phone Number" data type

The phone number format for this field is controlled by a client script. By default, it only supports phone numbers in local format for some western countries.

Follow these steps to customize this script to show the phone number in the local format of your own country.

For example, users in Japan enter "334561234" in the field and hope it will be converted to "(03) 3456-1234" in Japan local format automatically.

Steps:

  1. Navigate to the sys_ui_script.list table
  2. Create a new UI script record with this information:
    • API Name = formatting.js (Or any preferred name)
    • UI Type = Desktop
    • Active = true
  3. Open this client script copy it into the newly created record:

Script : 

/*! RESOURCE: /scripts/formatting.js */
function formatPhone(field) {
//  field.value = trim(field.value);
//  var ov = field.value;
//  var v = "";
//  var x = -1;
// if (0 < ov.length && '+' != ov.charAt(0)) {
//      var n = 0;
//      if ('1' == ov.charAt(0))
// ov = ov.substring(1, ov.length);
        // for (var i = 0; i < ov.length; i++) {
        //  var ch = ov.charAt(i);
        //  if (ch >= '0' && ch <= '9') {
        //      if (n == 0)
        //          v += "(";
        //      else if (n == 3)
        //          v += ") ";
        //      else if (n == 6)
        //          v += "-";
        //      v += ch;
        //      n++;
        //  }
        //  if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
        //      x = i;
        //      break;
        //  }
        // }
    //  if (x >= 0)
    //      v += " " + ov.substring(x, ov.length);
    //  if (n == 10 && v.length <= 40)
    //      field.value = v;
    // }
    return true;
}
function formatClean(num) {
    var sVal = '';
    var nVal = num.length;
    var sChar = '';
    var nChar = '';
    try {
        for (var i = 0; i < nVal; i++) {
            sChar = num.charAt(i);
            nChar = sChar.charCodeAt(0);
            if (sChar == '-' || sChar == getDecimalSeparator() || ((nChar >= 48) && (nChar <= 57)))
                sVal += num.charAt(i);
        }
    }
    catch (exception) {
        alertError("formatClean", exception);
    }
    return sVal;
}
function formatCurrency(num) {
    var sVal = '';
    var minus = '';
    if (num.lastIndexOf("-") == 0)
        minus = '-';
    if (num.lastIndexOf(".") < 0)
        num = num + '00';
    num = formatClean(num);
    sVal = minus + formatDollar(num, getGroupingSeparator()) + getDecimalSeparator() + formatCents(num);
    return sVal;
}
function formatNumber(num) {
    if (num.length == 0)
        return num;
    num = num + "";
    var sVal = '';
    var minus = '';
    var samount = '';
    try {
        if (num.lastIndexOf("-") == 0)
            minus = '-';
        num = formatClean(num);
        if (num.indexOf("-") == 0)
num = num.substring(1);
        num = "0" + num;
        var fraction = parseFraction(num + "");
        num = parseInt(num, 10);
        samount = num + "";
for (var i = 0; i < Math.floor((samount.length - (1 + i)) / 3); i++)
            samount = samount.substring(0, samount.length - (4 * i + 3)) + getGroupingSeparator() + samount.substring(samount.length - (4 * i + 3));
        if (fraction.length > 0) {
            fraction = getDecimalSeparator() + fraction;
            samount += fraction;
        }
    } catch (exception) {
        alertError("Format Number", exception);
    }
return minus + samount;
}
function parseFraction(num) {
    var index = num.indexOf(getDecimalSeparator());
    if (index == -1)
        return "";
    return num.substring(index + 1);
}
function formatCents(amount) {
    var cents = '';
    try {
        amount = parseInt(amount, 10);
        var samount = amount + "";
        if (samount.length == 0)
            return '00';
        if (samount.length == 1)
            return '0' + samount;
        if (samount.length == 2)
            return samount;
        cents = samount.substring(samount.length - 2, samount.length);
    }
    catch (exception) {
        alertError("Format Cents", e);
    }
    return cents;
}
function formatDollar(amount) {
    var samount = "";
    try {
        amount = parseInt(amount, 10);
        samount = amount + "";
        if (samount.length < 3)
            return 0;
        samount = samount.substring(0, samount.length - 2);
for (var i = 0; i < Math.floor((samount.length - (1 + i)) / 3); i++)
            samount = samount.substring(0, samount.length - (4 * i + 3)) + getGroupingSeparator() + samount.substring(samount.length - (4 * i + 3));
    }
    catch (exception) {
        alertError("Format Dollar", e);
    }
    return samount;
}
function padLeft(value, width, fill) {
    value = value + '';
    while (value.length < width)
        value = fill + value;
    return value;
}
function getDecimalSeparator() {
    if (g_user_decimal_separator)
        return g_user_decimal_separator;
    return ".";
}
function getGroupingSeparator() {
    if (g_user_grouping_separator)
        return g_user_grouping_separator;
    return ",";
}
function alertError(MethodName, e) {
    let gwt = new GwtMessage();
    if (e.description == null)
        alert(gwt.getMessage("{0} Exception: {1}", MethodName, e.message));
    else
        alert(gwt.getMessage("{0} Exception: {1}", MethodName, e.description));
}
function getFraction(num) {
    if (!num) return num;
num = formatClean(num);
    return parseFraction(num);
}
function getWholePart(num) {
    if (!num) return num;
    num = formatClean(num);
    let index = num.indexOf(getDecimalSeparator());
    if (index > -1)
        num = num.substring(0, index);
    return !num || num === '-' ? '0' : num;
}
;
ahmed24_0-1770799028360.png

 

View solution in original post

14 REPLIES 14

Dr Atul G- LNG
Tera Patron

Hi @ahmed-24 

what is the use case  change data type of the Phone Number field. It is not recommended because this field is used in On-Call, FSM, and CSM as well. If it is changed to a string, users would be able to enter anything, which would not be the correct behavior.

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

hello @Dr Atul G- LNG  

I would like to change the format of the Mobile phone (mobile_phone) field in the User [sys_user] table.

Currently, the phone numbers are displayed in the following format:
Example: 1(449) 260-6439

However, I would like the numbers to be displayed and stored in a simple numeric format like:
Example: 06212348725

Hi @ahmed-24 

What is the use case for converting this data type to a string? What are you trying to achieve by changing it?

Please consider that this field is not just a phone number field—it is used for on-call purposes, CSM, FSM, and other integrations where messages are sent to phone numbers. Changing the data type would disrupt all of these functionalities.

This data type is by design and serves a specific purpose, so it should not be changed. If you need additional flexibility, it would be better to create a new field and, through logic, convert it to a string and use it instead, rather than modifying the existing platform field.

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Chaitanya ILCR
Giga Patron

HI @ahmed-24 ,

setting the system property (glide.ui.format_phone) to false is working for me

if you still facing the issue try clearing the cache or try to use a different browser or try incognito mode

 

if you are still facing the issue

try this

create a client script on sys_user table

 

 

ChaitanyaILCR_3-1770741091053.png

 

function onLoad() {
    if (g_form.hasField('mobile_phone')) {
        var mp = g_form.getValue('mobile_phone')
        if (mp) {
            g_form.setValue('mobile_phone', mp.replace(/[^0-9]/g, ""));
        }
        var mobilePhone = g_form.getControl('mobile_phone');
        mobilePhone.setAttribute('onChange', '');
    }


    if (g_form.hasField('phone')) {
        var phone = g_form.getValue('phone')
        if (phone) {
            g_form.setValue('phone', phone.replace(/[^0-9]/g, ""));
        }
        var phoneElement = g_form.getControl('phone');
        phoneElement.setAttribute('onChange', '');
    }
}

 

 

also you would have to run a fix script or Background script to fix the existing data

var userGR = new GlideRecord("sys_user");
userGR.addEncodedQuery("mobile_phoneISNOTEMPTY^NQphoneISNOTEMPTY");
// userGR.setLimit(10);
userGR.query();
while (userGR.next()) {

    var mp = userGR.getValue('mobile_phone')
    if (mp)
        userGR.setValue('mobile_phone', mp.replace(/[^0-9]/g, ""))
    var phone = userGR.getValue('phone');
    if (phone)
        userGR.setValue('phone', phone.replace(/[^0-9]/g, ""))

	userGR.update()
}

result

 

ChaitanyaILCR_4-1770741213510.png

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

ahmed-24
Giga Guru

Resolution

For the old "Phone Number" data type

The phone number format for this field is controlled by a client script. By default, it only supports phone numbers in local format for some western countries.

Follow these steps to customize this script to show the phone number in the local format of your own country.

For example, users in Japan enter "334561234" in the field and hope it will be converted to "(03) 3456-1234" in Japan local format automatically.

Steps:

  1. Navigate to the sys_ui_script.list table
  2. Create a new UI script record with this information:
    • API Name = formatting.js (Or any preferred name)
    • UI Type = Desktop
    • Active = true
  3. Open this client script copy it into the newly created record:

Script : 

/*! RESOURCE: /scripts/formatting.js */
function formatPhone(field) {
//  field.value = trim(field.value);
//  var ov = field.value;
//  var v = "";
//  var x = -1;
// if (0 < ov.length && '+' != ov.charAt(0)) {
//      var n = 0;
//      if ('1' == ov.charAt(0))
// ov = ov.substring(1, ov.length);
        // for (var i = 0; i < ov.length; i++) {
        //  var ch = ov.charAt(i);
        //  if (ch >= '0' && ch <= '9') {
        //      if (n == 0)
        //          v += "(";
        //      else if (n == 3)
        //          v += ") ";
        //      else if (n == 6)
        //          v += "-";
        //      v += ch;
        //      n++;
        //  }
        //  if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
        //      x = i;
        //      break;
        //  }
        // }
    //  if (x >= 0)
    //      v += " " + ov.substring(x, ov.length);
    //  if (n == 10 && v.length <= 40)
    //      field.value = v;
    // }
    return true;
}
function formatClean(num) {
    var sVal = '';
    var nVal = num.length;
    var sChar = '';
    var nChar = '';
    try {
        for (var i = 0; i < nVal; i++) {
            sChar = num.charAt(i);
            nChar = sChar.charCodeAt(0);
            if (sChar == '-' || sChar == getDecimalSeparator() || ((nChar >= 48) && (nChar <= 57)))
                sVal += num.charAt(i);
        }
    }
    catch (exception) {
        alertError("formatClean", exception);
    }
    return sVal;
}
function formatCurrency(num) {
    var sVal = '';
    var minus = '';
    if (num.lastIndexOf("-") == 0)
        minus = '-';
    if (num.lastIndexOf(".") < 0)
        num = num + '00';
    num = formatClean(num);
    sVal = minus + formatDollar(num, getGroupingSeparator()) + getDecimalSeparator() + formatCents(num);
    return sVal;
}
function formatNumber(num) {
    if (num.length == 0)
        return num;
    num = num + "";
    var sVal = '';
    var minus = '';
    var samount = '';
    try {
        if (num.lastIndexOf("-") == 0)
            minus = '-';
        num = formatClean(num);
        if (num.indexOf("-") == 0)
num = num.substring(1);
        num = "0" + num;
        var fraction = parseFraction(num + "");
        num = parseInt(num, 10);
        samount = num + "";
for (var i = 0; i < Math.floor((samount.length - (1 + i)) / 3); i++)
            samount = samount.substring(0, samount.length - (4 * i + 3)) + getGroupingSeparator() + samount.substring(samount.length - (4 * i + 3));
        if (fraction.length > 0) {
            fraction = getDecimalSeparator() + fraction;
            samount += fraction;
        }
    } catch (exception) {
        alertError("Format Number", exception);
    }
return minus + samount;
}
function parseFraction(num) {
    var index = num.indexOf(getDecimalSeparator());
    if (index == -1)
        return "";
    return num.substring(index + 1);
}
function formatCents(amount) {
    var cents = '';
    try {
        amount = parseInt(amount, 10);
        var samount = amount + "";
        if (samount.length == 0)
            return '00';
        if (samount.length == 1)
            return '0' + samount;
        if (samount.length == 2)
            return samount;
        cents = samount.substring(samount.length - 2, samount.length);
    }
    catch (exception) {
        alertError("Format Cents", e);
    }
    return cents;
}
function formatDollar(amount) {
    var samount = "";
    try {
        amount = parseInt(amount, 10);
        samount = amount + "";
        if (samount.length < 3)
            return 0;
        samount = samount.substring(0, samount.length - 2);
for (var i = 0; i < Math.floor((samount.length - (1 + i)) / 3); i++)
            samount = samount.substring(0, samount.length - (4 * i + 3)) + getGroupingSeparator() + samount.substring(samount.length - (4 * i + 3));
    }
    catch (exception) {
        alertError("Format Dollar", e);
    }
    return samount;
}
function padLeft(value, width, fill) {
    value = value + '';
    while (value.length < width)
        value = fill + value;
    return value;
}
function getDecimalSeparator() {
    if (g_user_decimal_separator)
        return g_user_decimal_separator;
    return ".";
}
function getGroupingSeparator() {
    if (g_user_grouping_separator)
        return g_user_grouping_separator;
    return ",";
}
function alertError(MethodName, e) {
    let gwt = new GwtMessage();
    if (e.description == null)
        alert(gwt.getMessage("{0} Exception: {1}", MethodName, e.message));
    else
        alert(gwt.getMessage("{0} Exception: {1}", MethodName, e.description));
}
function getFraction(num) {
    if (!num) return num;
num = formatClean(num);
    return parseFraction(num);
}
function getWholePart(num) {
    if (!num) return num;
    num = formatClean(num);
    let index = num.indexOf(getDecimalSeparator());
    if (index > -1)
        num = num.substring(0, index);
    return !num || num === '-' ? '0' : num;
}
;
ahmed24_0-1770799028360.png