IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Asp.NetCore轻松学-业务重点-实现一个简单的手机号码验证

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

前言

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

1. 目前手机号段有哪些
  • 1.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

77_1.png

  • 2.1 将上面的号段加入配置文件 appsettings.json 中
{
  "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);
        }

  • 以上代码通过读取配置文件节点 phone-segment 并初始化 PhoneValidator 类,最后注入到 IServiceCollection 中,完成了初始化的工作
5. 在控制器中使用 PhoneValidator 进行验证
  • 5.1 示例代码
    [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 });
        }
    }

  • 5.2 运行项目,在浏览器中输入地址
http://localhost:33868/api/home/login?phone=86-13800138000

  • 5.3 输出结果
    77_2.png

结语

  • 通过上面的示例,可以实现对各种各样手机号码的控制,由于号段写在配置文件中,我们可以在业务扩展到时候去动态的增加号段,还可以针对各个地区去扩展 PhoneValidator 类,以实现切合业务的验证需求,从此,手机号码验证不再需要一刀切。

示例代码下载

https://github.com/lianggx/EasyAspNetCoreDemo/tree/master/Ron.PhoneTest

文章永久链接:https://tech.souyunku.com/?p=39089


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(100) 打赏



未经允许不得转载:搜云库技术团队 » Asp.NetCore轻松学-业务重点-实现一个简单的手机号码验证

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367