The CreatorCon Call for Content is officially open! Get started here.

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

Hi Ankur,

 

Thank you for sharing the code. We have a customized table containing a list of country codes, and there is a phone number field in the hardware table, which is assigned to different users. However, the current format of the phone number is +12345678900, without a space between the country code and the rest of the number. The requirement is to fetch the phone number and insert a space between the country code and the phone number, as shown in the example: +12 345678900.

Additionally, the length of the phone number can vary, either being 9 or 10 digits, and the length of the country code can differ as well. How will the above code account for the varying country code lengths and phone number lengths?

KoyelGuha_0-1737014179172.png

 

 

Thank you.

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @Koyel Guha 

As per requirement might be below code will help you.

var phoneNumber = '+41794XXXXXXX';

// List of country codes and their lengths
var countryCodes = {
    '+1': 2,    // USA, Canada
    '+44': 3,   // UK
    '+91': 3,   // India
    '+41': 3,   // Switzerland
    // Add more country codes and their lengths as needed
};

// Function to find the country code
function getCountryCode(phoneNumber) {
    for (var code in countryCodes) {
        if (phoneNumber.startsWith(code)) {
            return code;
        }
    }
    return null;
}

var countryCode = getCountryCode(phoneNumber);
if (countryCode) {
    var countryCodeLength = countryCodes[countryCode];
    var components = {
        IDDCC: phoneNumber.substr(0, countryCodeLength),
        NN: phoneNumber.substr(countryCodeLength)
    };
    gs.print(JSON.stringify(components));
} else {
    gs.print('Country code not found');
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hi Viraj,

 

Thank you for sharing the code.

We have a customized table containing a list of country codes, and there is a phone number field in the hardware table, which is assigned to different users. However, the current format of the phone number is +12345678900, without a space between the country code and the rest of the number. The requirement is to fetch the phone number and insert a space between the country code and the phone number, as shown in the example: +12 345678900.

Additionally, the length of the phone number can vary, either being 9 or 10 digits, and the length of the country code can differ as well. How will the above code account for the varying country code lengths and phone number lengths?

 

KoyelGuha_1-1737014206472.png

 

 

Thank you.