I was having lots of phone numbers (e.g. 1-123-456-7890) in my DB. What I have to do is, separate the country dialing code (in this case 1 which is for US/Canada) from the phone number.
I tried creating a JSON list of all countries and when loading the page, I separated the phone number and country code. It was working fine till I got some numbers which start with +
or having only 6 or 7 digits in the phone number (in which case there is no country code).
I have tried Google's GeoName API but it doesn't return what I was expecting. And I couldn't find any API for getting the country code from the phone number.
I was having lots of phone numbers (e.g. 1-123-456-7890) in my DB. What I have to do is, separate the country dialing code (in this case 1 which is for US/Canada) from the phone number.
I tried creating a JSON list of all countries and when loading the page, I separated the phone number and country code. It was working fine till I got some numbers which start with +
or having only 6 or 7 digits in the phone number (in which case there is no country code).
I have tried Google's GeoName API but it doesn't return what I was expecting. And I couldn't find any API for getting the country code from the phone number.
Share Improve this question edited Jan 24, 2022 at 14:47 lumio 7,5854 gold badges41 silver badges59 bronze badges asked Aug 21, 2017 at 12:07 JoomlerJoomler 2,7983 gold badges33 silver badges38 bronze badges 10- Why do you want to separate the country code from the phone number? – christopher Commented Aug 21, 2017 at 12:08
- for the UI purpose, and I wanted to add country list with dialing codes for better UX – Joomler Commented Aug 21, 2017 at 12:12
- well, there is no easy answer for this case, I would go with catching country code with regex, but if you want some example I need all possible formats you want to operate on – Kasia Commented Aug 21, 2017 at 12:13
-
I suspect you'll find this is more or less impossible. While you can tell that any number starting with a
+
will then have the international dialling code as the next digits, for any other number you won't be able to tell if it is a local number or an international number that is either missing the dialling prefix or has a country specific dialling prefix on it. – Quentin Commented Aug 21, 2017 at 12:14 - 1 @Kejt — How would you write a regex that would match country codes? – Quentin Commented Aug 21, 2017 at 12:15
2 Answers
Reset to default 10This is one of the sort of problems which is quite plicated. I would suggest to use a library like libphonenumber-js.
I created a little helper function, that uses the US country code by default:
function getCountryCode( input ) {
// Set default country code to US if no real country code is specified
const defaultCountryCode = input.substr( 0, 1 ) !== '+' ? 'US' : null;
let formatted = new libphonenumber.asYouType( defaultCountryCode ).input( input );
let countryCode = '';
let withoutCountryCode = formatted;
if ( defaultCountryCode === 'US' ) {
countryCode = '+1';
formatted = '+1 ' + formatted;
}
else {
const parts = formatted.split( ' ' );
countryCode = parts.length > 1 ? parts.shift() : '';
withoutCountryCode = parts.join( ' ' );
}
return {
formatted,
withoutCountryCode,
countryCode,
}
}
console.log( getCountryCode( '1-123-456-7890' ) );
console.log( getCountryCode( '+12133734' ) );
console.log( getCountryCode( '+49300200100' ) );
console.log( getCountryCode( '621234567' ) );
<script src="https://cdnjs.cloudflare./ajax/libs/libphonenumber-js/0.4.27/libphonenumber-js.min.js"></script>
You just need to have a list of all countries with codes and then loop it and verify if the number starts with the code.
The problem with the libphonenumber-js is that it verifies the number with and without the country code, then sometimes there are more than one possibility for a number.
If you want to strictly find the code and the code is not omitted go with the code below.
handleInternationalPhoneNumber(number){
const possibleDDIs = [];
allCountriesPhonCodes.forEach((country) => {
if (number.startsWith(country.code)) {
possibleDDIs.push(country);
}
});
return possibleDDIs;
}