简体   繁体   English

如何区分国家/地区代码形式的电话号码?

[英]How to separate country code form Phone number?

I was having lots of phone numbers [eg: 1-123-456-7890] in my DB. 我的数据库中有很多电话号码[例如:1-123-456-7890]。 What I have to do is, separate country dialing code [in this case 1 which is for US/Canada] from the phone number. 我要做的是,将电话号码中的国家/地区代码[在本例中为1,用于美国/加拿大]分开。

What I have tried is - I made a json list of all countries and when getting loading the page, I separated phone number and country code. 我尝试过的是-我列出了所有国家/地区的json列表,并在加载页面时将电话号码和国家/地区代码分开。 and it was working fine till I got some numbers which starts with '+' or having only 6 or 7 digits in the phone number(in this case there is no country code). 直到我得到一些以“ +”开头或电话号码中只有6或7位数字的电话(在这种情况下,没有国家/地区代码),它都可以正常工作。

I have tried google's GeoName API but it doesn't return what I was expecting. 我已经尝试过Google的GeoName API,但没有返回我期望的结果。 And i couldn't find any API for getting the country code form the phone number. 而且我找不到用于从电话号码获取国家/地区代码的任何API。

This is one of the sort of problems which is quite complicated. 这是非常复杂的问题之一。 I would suggest to use a library like libphonenumber-js . 我建议使用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.com/ajax/libs/libphonenumber-js/0.4.27/libphonenumber-js.min.js"></script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用电话号码保存国家代码? - How to save country code with phone number? 如何分别获取国家代码和电话号码并根据javascript中的国家代码验证ph号码的长度? - How to get country code and phone number separately and validate length of the ph number based on country code in javascript? 如何通过按单独的选项列表选择国家/地区,在电话字段中添加国家/地区电话号码 - How can i add country phone number in phone field by selecting countries by separate option list 如何使用正则表达式使用国家/地区代码格式化手机号码 - How to format mobile phone number with country code using regex 如何使用正则表达式验证电话号码中的国家/地区代码 - How to make regex that validate country code in phone number Laravel 8 x-form.input 标签中带有国家代码的电话号码表格 - Phone number form with country code in Laravel 8 x-form.input tag 为电话号码选择国家/地区代码时,如何将土耳其设为默认国家/地区? - How can I make Turkey the default country when choosing a country code for a phone number? 使用 intlTelInput 验证带有国家代码的电话号码 - Validation phone number with country code using intlTelInput 根据国家/地区代码验证电话号码 - Validating Phone Number based on country code 验证电话号码和国家/地区代码组合以验证Angular 4中的电话号码吗? - Validate Phone Number and Country Code combination for phone number validition in Angular 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM