ASP.NET Core Library – Google libphonenumber (Country Dial Code)
前言
Google libphonenumber 是 Java 的, ASP.NET Core 只是 port 过去而已.
以前在 angular2 学习笔记 ( translate, i18n 翻译 ) 有介绍过. 但后来就一直没怎么用过了.
参考
Github – Google libphonenumber
Github – libphonenumber-csharp
Stack Overflow – Listing all country codes of phone numbers
相关链接
Github – CountryCodes.json (JSON)
geonames.org (API)
Wikipedia – List of country calling codes
nuget 安装
dotnet add package libphonenumber-csharp
基本操作
Get Phone Number Information
var singaporeNumber = "+6590269356";
var phoneUtil = PhoneNumberUtil.GetInstance();
PhoneNumber number = phoneUtil.Parse(singaporeNumber, defaultRegion: "");
var regionCode = phoneUtil.GetRegionCodeForNumber(number); // SG
var isValidNumber = phoneUtil.IsValidNumber(number);
var internationalFormat = phoneUtil.Format(number, PhoneNumberFormat.INTERNATIONAL); //+65 9026 9356
var localFormat = phoneUtil.Format(number, PhoneNumberFormat.NATIONAL); // 9026 9356
当获取到一个用户输入的号码, 我们可以通过 PhoneNumberUtil 检查它是否 valid, 是属于什么国家, 还可以获取它的正规格式.
-RegionCode 指的是 Country String Code, 比如 Malaysia = MY, Singapore = SG
-CountryCode 指的是 Dial Code, 比如 Malaysia = 60, Singapore = 65
Parse 失败会报错,所以需要 try catch,即便 Parse 成功也代表就是 valid number,所以还得验证多一次哦。
Get All Code List Information
var phoneUtil = PhoneNumberUtil.GetInstance();
List<int> countryCodes = phoneUtil.GetSupportedCallingCodes().ToList(); // e.g. 60, 65
foreach (var countryCode in countryCodes)
{
var regionCode = phoneUtil.GetRegionCodeForCountryCode(countryCode); // e.g. MY, SG
try
{
var countryName = new Locale("", regionCode).GetDisplayCountry("en"); // e.g. Malaysia, Singapore
}
catch
{
// 247, 599, 211, 383, 800, 808, 870, 878, 881, 882, 883, 888, 979
// 上面这些 CountryCode/DialCode 的 RegionCode 对不上任何国家...我不知道为什么, 但 Wikipedia 也找不到这些 DialCode
noCountryNames.Add(countryCode);
}
}
上面是拿 country code / dial code > 转去 region code > 获取 country name
先拿 region code > 转去 country code 也可以
var regionCodes = phoneUtil.GetSupportedRegions(); // e.g. MY, SG
var countryCode = phoneUtil.GetCountryCodeForRegion("MY"); // 60
ASP.NET Core Library – Google libphonenumber (Country Dial Code)的更多相关文章
- ASP.NET Core 使用 Google 验证码(reCAPTCHA v3)代替传统验证码
写在前面 友情提示: Google reCAPTCHA(v3下同) 的使用不需要"梯子",但申请账号的时候需要! Google reCAPTCHA 的使用不需要"梯子&q ...
- ASP.NET Core 使用 Google 验证码(Google reCAPTCHA)
关心最多的问题,不FQ能不能用,答案是能.Google官方提供额外的域名来提供服务,国内可以正常使用. 一. 前言 验证码在我们实际的生活场景中非常常见,可以防止恶意破解密码.刷票.论坛灌水.刷注册等 ...
- Create an ASP.NET Core web app in Visual Studio Code
https://www.microsoft.com/net/core#windowscmd https://download.microsoft.com/download/B/9/F/B9F1AF57 ...
- [转]ASP.NET Core 1 Deploy to IIS
本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...
- 《ASP.NET Core 高性能系列》致敬伟大的.NET斗士甲骨文!
写在开始 三年前,曾写过一篇文章:从.NET和Java之争谈IT这个行业,当时遭到某些自认为懂得java就了不起的Javaer抨击, 现在可以致敬伟大的.NET斗士甲骨文了 (JDK8以上都需要收费, ...
- ASP.NET Core 介绍
原文:Introduction to ASP.NET Core 作者:Daniel Roth.Rick Anderson.Shaun Luttin 翻译:江振宇(Kerry Jiang) 校对:许登洋 ...
- 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用
Setting Up Your Development Environment 设置你的开发环境 To setup your development machine download and inst ...
- 005.Getting started with ASP.NET Core MVC and Visual Studio -- 【VS开发asp.net core mvc 入门】
Getting started with ASP.NET Core MVC and Visual Studio VS开发asp.net core mvc 入门 2017-3-7 2 分钟阅读时长 本文 ...
- ASP.NET Core 2.0 自定义 _ViewStart 和 _ViewImports 的目录位置
在 ASP.NET Core 里扩展 Razor 查找视图目录不是什么新鲜和困难的事情,但 _ViewStart 和 _ViewImports 这2个视图比较特殊,如果想让 Razor 在我们指定的目 ...
- User Authentication with Angular and ASP.NET Core
User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...
随机推荐
- Docker开始收费了,开始转学podman【第一篇podman容器的安装和基本操作】
podman 什么是Podman?Podman是无守护程序容器引擎,用于在Linux系统上开发,管理和运行OCI容器.容器可以以root用户或无根模式运行.简而言之:`alias docker = p ...
- python 列表append和 的区别??
python列表中的合并 python列表中append方法是给列表增加一个元素,而'+='是增加上该符号后边的元素,类似于extend方法 不知道对错,先记下来.我学的append方法是在列表最后追 ...
- Python用shp文件裁剪多个遥感影像的方法
本文介绍基于Python中ArcPy模块,基于矢量数据范围,对大量栅格遥感影像加以批量裁剪掩膜的方法. 首先,话不多说,本文所需要的代码如下所示. # -*- coding: utf-8 -* ...
- springsecurity:权限与异常处理
权限即不同用户可以使用不同功能 实现前置: 在上一次登录与校验中,我们将authentication存入到SecurityContextHolder中,后续我们需要从FilterSecurityInt ...
- Redis内存回收与缓存问题
内存回收: 1.过期key处理 通过expire命令给key设置ttl Redis本身是KV型数据库,所有数据都存在RedisDB结构体中,其中有两张哈希表 dict:用于存放KV(这里K是K,V是V ...
- Charles 4.6 小茶杯 网络抓包工具
下载官网: https://www.charlesproxy.com/download 破解网站: Charles破解工具 (zzzmode.com)
- Android studio报错:Failed to allocate a 3213123 byte allocation with 31231 free bytes and 189MB ontil 0OM
这个问题是运行内存超了 在AndroidManifest中加入 android:hardwareAccelerated="false"android:largeHeap= &quo ...
- 【Mybatis-Plus】联表分页查询实现
参考文章: https://blog.csdn.net/weixin_43847283/article/details/125822614 上上周写的SQL案例确实可以重构,所以搬到Demo里面测试看 ...
- 【Hadoop】3.3.1版本部署
硬件环境: 虚拟机环境配置 硬件:1CPU + [RAM]4G + [DISK]128G 系统:Centos7 x64 这里没有像尚硅谷一样做地址映射解析,直接取IP(我太懒) 192.168.242 ...
- 【Shiro】02 shiro.ini文件分析
[什么是INI文件?] INI 全称:Initialization File 初始文件 Window系统文件扩展名 Shiro 使用时可以连接数据库,也可以不连接数据库. 当不使用数据库时,需要配置S ...