前言

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

相关链接

countrycode.org

Github – CountryCodes.json (JSON)

geonames.org (API)

Wikipedia – List of country calling codes

Playground

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)的更多相关文章

  1. ASP.NET Core 使用 Google 验证码(reCAPTCHA v3)代替传统验证码

    写在前面 友情提示: Google reCAPTCHA(v3下同) 的使用不需要"梯子",但申请账号的时候需要! Google reCAPTCHA 的使用不需要"梯子&q ...

  2. ASP.NET Core 使用 Google 验证码(Google reCAPTCHA)

    关心最多的问题,不FQ能不能用,答案是能.Google官方提供额外的域名来提供服务,国内可以正常使用. 一. 前言 验证码在我们实际的生活场景中非常常见,可以防止恶意破解密码.刷票.论坛灌水.刷注册等 ...

  3. 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 ...

  4. [转]ASP.NET Core 1 Deploy to IIS

    本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...

  5. 《ASP.NET Core 高性能系列》致敬伟大的.NET斗士甲骨文!

    写在开始 三年前,曾写过一篇文章:从.NET和Java之争谈IT这个行业,当时遭到某些自认为懂得java就了不起的Javaer抨击, 现在可以致敬伟大的.NET斗士甲骨文了 (JDK8以上都需要收费, ...

  6. ASP.NET Core 介绍

    原文:Introduction to ASP.NET Core 作者:Daniel Roth.Rick Anderson.Shaun Luttin 翻译:江振宇(Kerry Jiang) 校对:许登洋 ...

  7. 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用

    Setting Up Your Development Environment 设置你的开发环境 To setup your development machine download and inst ...

  8. 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 分钟阅读时长 本文 ...

  9. ASP.NET Core 2.0 自定义 _ViewStart 和 _ViewImports 的目录位置

    在 ASP.NET Core 里扩展 Razor 查找视图目录不是什么新鲜和困难的事情,但 _ViewStart 和 _ViewImports 这2个视图比较特殊,如果想让 Razor 在我们指定的目 ...

  10. User Authentication with Angular and ASP.NET Core

    User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...

随机推荐

  1. [Unity] Dreamteck Splines实现沿路径移动功能

    Dreamteck Splines实现沿路径移动功能 最近有一个"让物体沿固定路径移动"的需求,因此接触到了Dreamteck Splines插件. Dreamteck Splin ...

  2. 使用SVG做模型贴图的思路

    大多数情况下,三维模型使用PNG,JPG作为模型的贴图,当然为了性能优化,有时候也会使用压缩贴图来提高渲染效率和较少GPU压力. 今天提供一种新的思路,使用SVG作为模型的贴图,可以达到动态调整图片精 ...

  3. oeasy 教您玩转 linux 之 010301 电子宠物 pet

    我们来回顾一下 上一部分我们都讲了什么? 中️文诗词fortune=zh的使用 建立自己的彩色诗词库 通过pip把输出结果交给cowsay或boxes 我们这次讲一讲桌面的一些应用 oneko 我们可 ...

  4. [oeasy]python0037_电传打字机_打印头_print_head_carriage_词源

    换行回车 回忆上次内容 上次我们 diy了 自己的小动物 还可以 让小动物 变色.报时 还可以 说些话 这很亚文化 很酷炫的亚文化 不是吗? 回忆一下 最开始 研究报时 的 时候 回到 本行行头 的 ...

  5. C#封装HttpClient工具类库(.NET4.5以上)

    1.Json字符串实体转换扩展方法,依赖Json.Net包 /// <summary> /// Json扩展方法 /// </summary> public static cl ...

  6. Java 线程池之Jetty 线程池学习总结

    Java 线程池之Jetty 线程池学习总结 前提 Jetty 11.0.x 为什么是Jetty? Java提供4中创建线程池的快捷方式 Executors.newFixedThreadPool(); ...

  7. WrodPress基础之前期7个必要的基本设置

    不管使用宝塔面板搭建WordPress还是1Panel面板的方式,一个新WordPress网站需要做一些基本设置才能正式的去设计页面,填充网站内容. 1. 确保网站勾选"建议搜索引擎不收录& ...

  8. 全网最适合入门的面向对象编程教程:29 类和对象的Python实现-断言与防御性编程和help函数的使用

    全网最适合入门的面向对象编程教程:29 类和对象的 Python 实现-断言与防御性编程和 help 函数的使用 摘要: 在 Python 中,断言是一种常用的调试工具,它允许程序员编写一条检查某个条 ...

  9. openeuler linux内核4.19安装(centos 同理)

    linux内核安装: 安装内核步骤 下载相应内核版本[我这里用的是linux-4.19.90.tar.gz] 下载网址:https://mirrors.edge.kernel.org/pub/linu ...

  10. 【Java】Input,Output,Stream I/O流 01 概述 & 4个基础流

    Input,Output,Stream IO流 I/O Input/Output缩写.I/O技术用于处理设备之间的数据传输,读写文件,网络通讯 Java程序对于数据的操作以Stream流的形式进行 i ...