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

Ankur Bawiskar
Tera Patron

@ahmed-24 

you can't control that as OOTB the field type for mobile_phone is Phone Number

It will format that value.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar so the solution ? change type to string ? but it's not clean this solution !

 

i have found this article : https://www.servicenow.com/docs/washingtondc/platform-administration/r_ConfigureE.164PhoneNumberFiel... but isn't helpfull.

@ahmed-24 

I will recommend not to change field type as you are touching OOTB field type.

Please inform customer about this OOTB behavior.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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