身份证号码验证

开发过程中常用的身份证号码验证:    

第一种验证:IsIdCard方法

632623198512079641 -->返回 true      63262319851207964 -->返回true

第二种验证:CheckIDCard方法

632623198512079641 -->返回 true      63262319851207964 -->返回False




代码(需要添加引用

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

        /// <summary>
        /// 验证身份证是否合法  15 和  18位两种
        /// </summary>
        /// <param name="idCard">要验证的身份证</param>
        public static bool IsIdCard(string idCard)
        {
            if (string.IsNullOrEmpty(idCard))
            {
                return false;
            }


            if (idCard.Length == 15)
            {
                return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
            }
            else if (idCard.Length == 18)
            {
                return Regex.IsMatch(idCard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$", RegexOptions.IgnoreCase);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断身份证号码是否正确(更精确)
        /// </summary>
        /// <param name="socialNo">身份证号码</param>
        /// <returns></returns>
        public static bool CheckIDCard(string socialNo)
        {

            if (socialNo.Length != 15 && socialNo.Length != 18)
            {
                return (false);
            }

            int[] area = new int[] { 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 71, 81, 82, 91 };

            if (!area.Contains(Convert.ToInt32(socialNo.Substring(0, 2))))
            {
                return (false);
            }

            if (socialNo.Length == 15)
            {
                Regex pattern = new Regex(@"^\d{15}$");
                if (pattern.Matches(socialNo).Count <= 0)
                {

                    return (false);
                }
                var birth = Convert.ToInt32("19" + socialNo.Substring(6, 2));
                string month = socialNo.Substring(8, 2);
                var day = Convert.ToInt32(socialNo.Substring(10, 2));
                switch (month)
                {
                    case "01":
                    case "03":
                    case "05":
                    case "07":
                    case "08":
                    case "10":
                    case "12":
                        if (day > 31)
                        {
                            return false;
                        }
                        break;
                    case "04":
                    case "06":
                    case "09":
                    case "11":
                        if (day > 30)
                        {
                            return false;
                        }
                        break;
                    case "02":
                        if ((birth % 4 == 0 && birth % 100 != 0) || birth % 400 == 0)
                        {
                            if (day > 29)
                            {

                                return false;
                            }
                        }
                        else
                        {
                            if (day > 28)
                            {

                                return false;
                            }
                        }
                        break;
                    default:

                        return false;
                }
                var nowYear = DateTime.Now.Year;
                if (nowYear - birth.ToInt() < 15 || nowYear - birth.ToInt() > 100)
                {

                    return false;
                }
                return (true);
            }

            int[] Wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

            var lSum = 0;
            var nNum = 0;
            var nCheckSum = 0;

            for (int i = 0; i < 17; ++i)
            {
                if (socialNo.Substring(i, 1).ToInt() < 0 || socialNo.Substring(i, 1).ToInt() > 9)
                {
                    return (false);
                }
                else
                {
                    nNum = socialNo.Substring(i, 1).ToInt() - 0;
                }
                lSum += nNum * Wi[i];
            }

            if (socialNo.Substring(17, 1).ToUpper() == "X")
            {
                lSum += 10 * Wi[17];
            }
            else if (socialNo.Substring(17, 1).ToInt() < 0 || socialNo.Substring(17, 1).ToInt() > 9)
            {
                return (false);
            }
            else
            {
                lSum += (socialNo.Substring(17, 1).ToInt() - 0) * Wi[17];
            }
            if ((lSum % 11) == 1)
            {
                return true;
            }
            else
            {
                return (false);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值