[英]How to deduce Country Code from Dial code?
I have a custom form made with Vue there is a field for a mobile number where I am using Vue Int Tel input package for country code dropdown with flags.我有一个用 Vue 制作的自定义表单,有一个手机号码字段,我在其中使用 Vue Int Tel 输入 package 用于带有标志的国家代码下拉列表。
I got a country Dial code value for the selected input from the country code dropdown.我从国家代码下拉列表中获得了所选输入的国家拨号代码值。 I want to get the country code from the dialing code value.
我想从拨号代码值中获取国家代码。 For example, If India is the selected country and I had a dial code value +91 How should I deduce Country code ie IN from that particular dial code value?
例如,如果印度是选定的国家,并且我的拨号代码值为 +91,我应该如何从该特定拨号代码值推断国家代码,即 IN?
**I'm able to get both values separately, but what I can't able to do is deducing country code from dial code. **我可以分别获得这两个值,但我不能做的是从拨号代码中推断出国家代码。
Any Help will much be appreciated!任何帮助将不胜感激!
Since vue-tel-input
internally uses libphonenumber-js - you can use it, too:由于
vue-tel-input
内部使用libphonenumber-js - 你也可以使用它:
<template>
<vue-tel-input ref="tel" v-model="phone" />
</template>
import parsePhoneNumberFromString from 'libphonenumber-js';
export default
{
data()
{
return {
phone: '',
};
},
methods:
{
getCountryCode()
{
// vue-tel-input does not update the model if you have a default country,
// so we have to access its internal representation
// To avoid accessing internal data of vue-tel-input, you can either not use
// a default country, or provide the default country as a 2nd argument to the
// parsing function - parsePhoneNumberFromString(this.phone, this.defaultCountry)
const result = parsePhoneNumberFromString((this.$refs.tel || {})._data.phone);
return result.country;
}
}
Don't forget to add the package to your project npm install libphonenumber-js
.不要忘记将 package 添加到您的项目
npm install libphonenumber-js
。
You could just create a object with each country code and number prefix like this:您可以创建一个 object,每个国家/地区代码和数字前缀如下:
country_codes = {
'+91': 'IN',
'+46': 'SE',
...
}
And the use this to "translate" number prefix to two digit country code.并使用它来将号码前缀“翻译”为两位数的国家代码。
country_codes['+46'] //returns 'SE'
Here you can find all country codes: https://countrycode.org/在这里您可以找到所有国家/地区代码: https://countrycode.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.