Asp.NetCore实现一个简单的手机号码验证

时间:2021-09-03 18:56:25   收藏:0   阅读:30

前言

????本文纯干货,直接拿走使用,不用付费。在业务开发中,手机号码验证是我们常常需要面对的问题,目前市场上各种各样的手机号码验证方式,比如正则表达式等等,本文结合实际业务场景,在业务级别对手机号码进行严格验证;同时增加可配置方式,方便业务扩展,代码非常简单,扩展非常灵活。

 

1. 目前手机号段有哪些
 "中国电信": "133,153,189,180,181,177,173,199,174,141",
 "中国移动": "139,138,137,136,135,134,159,158,157,150,151,152,147,188,187,182,183,184,178,198",
 "中国联通": "130,131,132,146,156,155,166,186,185,145,175,176",
 "虛拟运营商": "170,171",
 "内部号码": "123"

 

2. 建立一个测试项目 Ron.PhoneTest

技术分享图片

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "phone-segment": {
    "中国电信": "133,153,189,180,181,177,173,199,174,141",
    "中国移动": "139,138,137,136,135,134,159,158,157,150,151,152,147,188,187,182,183,184,178,198",
    "中国联通": "130,131,132,146,156,155,166,186,185,145,175,176",
    "虛拟运营商": "170,171",
    "内部号码": "123"
  }
}

 

3. 建立一个检查类,负责初始化号段库和校验的工作
    public class PhoneValidator
    {
        private static readonly Regex checktor = new Regex(@"^1\d{10}$");
        public IDictionary segment = null;

        public PhoneValidator(IDictionary segment)
        {
            this.segment = segment;
        }

        public bool IsPhone(ref string tel)
        {
            if (string.IsNullOrEmpty(tel))
            {
                return false;
            }

            tel = tel.Replace("+86-", "").Replace("+86", "").Replace("86-", "").Replace("-", "");
            if (!checktor.IsMatch(tel))
            {
                return false;
            }
            string s = tel.Substring(0, 3);
            if (segment.Count > 0 && !segment.Contains(s))
            {
                return false;
            }

            return true;
        }
    }

 

4. 通过 Startup.cs 实现读取配置和注入,以便系统使用
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            CreatePhoneValidator(services);
        }

        private void CreatePhoneValidator(IServiceCollection services)
        {
            Hashtable segment = new Hashtable();
            var coll = Configuration.GetSection("phone-segment").GetChildren();
            foreach (var prefix in coll)
            {
                if (string.IsNullOrEmpty(prefix.Value))
                    continue;
                foreach (var s in prefix.Value.Split(‘,‘))
                    segment[s] = s;
            }
            var pv = new PhoneValidator(segment);
            services.AddSingleton<PhoneValidator>(pv);
        }

 

5. 在控制器中使用 PhoneValidator 进行验证
    [Route("api/home")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        PhoneValidator validator = null;
        public HomeController(PhoneValidator pv)
        {
            validator = pv;
        }

        [HttpGet("login")]
        public IActionResult Login(string phone)
        {
            bool accept = validator.IsPhone(ref phone);
            return new JsonResult(new { phone, accept });
        }
    }
http://localhost:33868/api/home/login?phone=86-13800138000

结语

原文:https://www.cnblogs.com/chenguopa/p/15222653.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!