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 ...
随机推荐
- P2P应用
对等连接(peer to peer)文件分发的分析: 传统客户-服务器模式:用时与文件量成正比 P2P模式:随文件量增大而用时趋于一个极限. P2P工作方式有三: 集中式索引:客户访问服务器所需数据在 ...
- vue 理解yarn start 和yarn dev的区别
yarn dev,当文件变动后,会自动重启. yanr start不会自动重启 nodemon会监听文件变动,跟yarn dev和yarn start无关.
- [oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
直接运行 回忆上次内容 我们把两个程序整合起来了 可以持续输出当前时间 每秒都更新 编辑 但是我想在 shell 里面 只输入文件名(./sleep.py)并回车 就能不断输出时间 可能吗? ...
- Jetpack Compose学习(12)——Material Theme的主题色切换
原文:Jetpack Compose学习(12)--Material Theme的主题色切换-Stars-One的杂货小窝 闲着无事研究了下Jetpack Compose M3 主题切换效果 本系列以 ...
- 界面自动化测试录制工具,让python selenium自动化测试脚本开发更加方便
自动化测试中,QTP和selenium IDE都支持浏览器录制与回放功能,简单的来说就像一个记录操作步骤的机器人,可以按照记录的步骤重新执行一遍,这就是脚本录制.个人觉得传统录制工具有些弊端,加上要定 ...
- docker centos8 java8 mysql8 部署springboot项目
docker centos8 java8 mysql8 部署springboot项目 一,用idea将springboot项目打成jar包 二,将打的jar包用xshell的rz上传到docker的c ...
- 【MySQL】二级MySQL考试 救场帮助表
周六去考二级,应用第一题就是添加外键约束 草,写了半天老说语法不对,然后急中生智,觉得默认的库里应该有文档说明表 以下是SQL查询过程: -- 猜测是在mysql库里面 mysql> USE m ...
- 如何租GPU:一个价格还算OK的云GPU服务器租赁公司
一个价格还算OK的云GPU服务器租赁公司. 地址: https://www.gpushare.com/
- Ubuntu18.04 系统环境下 vscode中忽略pylint某些错误或警告
相关: ubuntu18.04系统环境下使用vs code安装pylint检查python的代码错误 ====================================== 假设已经在前文(ht ...
- mybatis-plus系统化学习之查询专题
1.背景 查询在实际生产中用得最多,也最灵活. 2.查询案例 表结构: CREATE TABLE `sys_user` ( `id` int(32) NOT NULL AUTO_INCREMENT C ...