![](/img/trans.png)
[英]Getting all country codes from libphonenumber-js and looping it out
[英]can't identify country code by typing country calling code using libphonenumber-js for US
我正在使用libphonenubmer-js
以在国家呼叫代码步骤中键入电话号码时显示国家国旗:
const asYouType = new AsYouType();
asYouType.input(`+${callingCode}`);
console.log({ asYouType.country })
问题是对于法国号码( +33 | 33
前缀), FR
会立即被识别。 但是对于美国( +1 | 1
前缀),它不会。
反正周围有吗? w/o 输入整数?
原因是 +1 前缀被更多国家使用,而不仅仅是美国。 见https://en.wikipedia.org/wiki/List_of_country_calling_codes
是的,确实这是一个鸽洞原理案例。
决定使用第一个可行的选项:
const countryCallingCode = e.target.value;
const asYouType = new AsYouType();
asYouType.input(`+${countryCallingCode}`);
let currentCountyCode = asYouType.country;
if (!currentCountyCode) {
const matchingCountriesArray = asYouType.metadata.metadata.country_calling_codes[
countryCallingCode
];
if (matchingCountriesArray) {
currentCountyCode = matchingCountriesArray[0];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.