Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to separate country code and number with a space from a phone number

Koyel Guha
Tera Contributor

There is a requirement to separate a phone number and country code with a space. We have a customized table where we have different country codes with different digits and different phone number digits.

 

There is a sample code for specific country code and phone number length. Can you suggest how it will work for all the country codes and phone n umber length.

 

var phoneNumber = '+41794XXXXXXX';

var countryCodeLength = phoneNumber.length - 10;

var components = {

    IDDCC: phoneNumber.substr(0, countryCodeLength),

    NN: phoneNumber.substr(countryCodeLength, phoneNumber.length)

};

    gs.print(JSON.stringify(components));

 

Please suggest.

Thanks.

7 REPLIES 7

PritamG
Mega Guru

To make it dynamic for all country codes and phone number lengths in ServiceNow, you need a way to determine the country code length. Use a predefined list of country code lengths or detect patterns dynamically if possible.

Solution:

var phoneNumber = '+41794XXXXXXX';
var countryCodeLength = detectCountryCodeLength(phoneNumber); 

var components = {
    IDDCC: phoneNumber.substr(0, countryCodeLength),
    NN: phoneNumber.substr(countryCodeLength)
};

gs.print(JSON.stringify(components));

function detectCountryCodeLength(phoneNumber) {
    var countryCodeRegex = /^\+\d{1,4}/;
    var match = phoneNumber.match(countryCodeRegex);
    return match ? match[0].length : 0; 
}

 

Uses regex to detect country code dynamically.

Supports varying country code and phone number lengths without hardcoding.

Returns separated IDDCC(country code) and NN(national number).

 

Community Alums
Not applicable

Hey @Koyel Guha 

Use the following function to saparate county code from number:

 

function separateCountryCodeAndNumber(phoneNumber) {

// Match the country code and phone number (assuming the country code is 1-4 digits long)

const regex = /^(\+\d{1,4})(\d{7,15})$/;

// Apply the regex to the input phone number

const match = phoneNumber.match(regex);

if (match) {

// Return the formatted string with a space between the country code and the number

return match[1] + ' ' + match[2];

} else {

return "Invalid phone number format.";

}

 

separateCountryCodeAndNumber("+123456789012");

O/p: +12 3456789012

Hi Nilesh,

You have specified the regex to 4 digits. There are other country codes length, how it will identify which part is the country code and which part is the phone number length. I have tried the code in background script, the code is not getting executed. Can you please help on this.

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Koyel Guha 

what all country codes are there?

try this

var phoneNumber = '+41794XXXXXXX';

// Regular expression to match the country code and phone number
var regex = /^(\+\d{1,4})(\d{6,15})$/;
var match = phoneNumber.match(regex);

if (match) {
    var components = {
        IDDCC: match[1], // Country code
        NN: match[2]     // Phone number
    };
    gs.print(JSON.stringify(components));
} else {
    gs.print('Invalid phone number format');
}

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