最给力的验证身份证号代码,包括位数、生日日期、校验和 【C#】

自己用的身份证号验证函数

  1. 在项目中,请先添加Regex类的引用:
    using System.Text.RegularExpressions;
  2. 日期验证函数
            /// <summary>
            /// 可匹配
            /// yyyyMMdd
            /// yyyy-MM-dd
            /// yyyy/MM/dd
            /// yyyy.MM.dd
            /// yyyy_MM_dd
            /// 且能过滤闰年
            /// </summary>
            /// <param name="txt"></param>
            /// <returns></returns>
            public static bool IsDate(string txt)
            {
                Regex objReg = new Regex(@"((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(10|12|0?[13578])([-\/\._]?)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(11|0?[469])([-\/\._]?)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._]?)(0?2)([-\/\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([3579][26]00)([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([1][89][0][48])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([2-9][0-9][0][48])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([1][89][2468][048])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([1][89][13579][26])([-\/\._]?)(0?2)([-\/\._]?)(29)$)|(^([2-9][0-9][13579][26])([-\/\._]?)(0?2)([-\/\._]?)(29)$))");
                return objReg.IsMatch(txt);
            }
  3. 身份证号验证函数
            /// 验证身份证
            /// 1.校验生日
            /// 2.校验最后一位(校验和)
            /// </summary>
            /// <param name="txt"></param>
            /// <returns></returns>
            public static bool IsIdentityCard(string txt)
            {
                Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
                string birthday = string.Empty;
                if (!objReg.IsMatch(txt))
                {
                    return false;
                }
    
                if (txt.Length == 15)
                {
                    //取生日
                    birthday = "19" + txt.Substring(7, 6);
                    return IsDate(birthday);
                }
                else if (txt.Length == 18)
                {
                    //取生日
                    birthday = txt.Substring(6, 8);
                    if (IsDate(birthday))
                    {
                        // 校验表
                        int[] check = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                        char[] checkSum = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
                        // 校验源
                        string checkSource = txt.Substring(0, 17);
                        // 校验源转换成数字
                        List<int> source = new List<int>();
                        for (int i = 0; i < checkSource.Length; i++)
                        {
                            source.Add(Convert.ToInt32(checkSource.Substring(i, 1)));
                        }
                        // 校验源的校验和
                        string checkLast = txt.Substring(17);
                        int sum = 0;
                        // 对应项求积,再把所有积求和
                        for (int i = 0; i < source.Count; i++)
                        {
                            sum += source[i] * check[i];
                        }
                        // 取余
                        int remainder = sum % 11;
                        if (string.Equals(checkLast, checkSum[remainder].ToString()))
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    return false;
                }
                return false;
            }
    	

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值