身份證驗證並計算

 public static class IdCardHelper
    {
        static readonly int[] Weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        static readonly char[] VCode = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        private const string Address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
        //定義成年人標準
        private const int AuditAge = 18;

        /// <summary>
        /// 身份證號碼檢查,返回用戶的年齡,生日,性別,驗證是否合法等。
        /// </summary>
        /// <param name="idCardNumList"></param>
        /// <returns></returns>
        public static List<IdCardUserInfo> CheckIdentityCard(List<string> idCardNumList)
        {
            var list = new List<IdCardUserInfo>();
            if (idCardNumList == null || idCardNumList.Count <= 0)
                return list;
            foreach (var idCardNum in idCardNumList)
            {
                var user = new IdCardUserInfo { IdCardNum = idCardNum };
                ProcessIdCard(idCardNum, user);
                list.Add(user);
            }
            return list;
        }

        static void ProcessIdCard(string idCardNum, IdCardUserInfo user)
        {
            try
            {
                bool valid;
                if (idCardNum.Length == 18)
                {
                    var age = GetAge(idCardNum.Substring(6, 4));
                    var birthday = idCardNum.Substring(6, 4) + "-" + idCardNum.Substring(10, 2) + "-" +
                                   idCardNum.Substring(12, 2);
                    if (!CheckValidCode(idCardNum.Substring(0, 17), idCardNum[17]))
                        valid = false;
                    else if (!CheckACode(idCardNum.Substring(0, 2)))
                        valid = false;
                    else if (age == 0)
                        valid = false;
                    else
                        valid = true;
                    user.IsValid = valid;
                    if (valid)
                    {
                        int g = Convert.ToInt32(idCardNum.Substring(14, 3));
                        if (g % 2 == 0)
                        {
                            user.Gender = 1;
                        }
                        else
                        {
                            user.Gender = 0;
                        }
                        user.Age = age;
                        user.IsAdult = age >= AuditAge;
                        user.BirthDay = Convert.ToDateTime(birthday);
                    }
                }
                else if (idCardNum.Length == 15)
                {
                    var age = GetAge("19" + idCardNum.Substring(6, 2));
                    var birthday = "19" + idCardNum.Substring(6, 2) + "-" + idCardNum.Substring(8, 2) + "-" +
                                   idCardNum.Substring(10, 2);
                    if (!CheckACode(idCardNum.Substring(0, 2)))
                        valid = false;
                    else if (age == 0)
                        valid = false;
                    else
                        valid = true;
                    user.IsValid = valid;
                    if (valid)
                    {
                        int g = Convert.ToInt32(idCardNum.Substring(12, 3));
                        if (g % 2 == 0)
                        {
                            user.Gender = 1;
                        }
                        else
                        {
                            user.Gender = 0;
                        }
                        user.Age = age;
                        user.IsAdult = age >= AuditAge;
                        user.BirthDay = Convert.ToDateTime(birthday);
                    }
                }
                else
                {
                    user.IsValid = false;
                }
            }
            catch (Exception ex)
            {
                user.IsValid = false;
            }
        }

        static bool CheckValidCode(string input17, char last)//檢查最後一位校驗碼  
        {
            if (last == CheckValidCode(input17))
                return true;
            return false;
        }
        static char CheckValidCode(string input17)//檢查最後一位校驗碼  
        {
            int sum = 0, cur;
            for (int i = 0; i < 17; i++)
            {
                cur = Convert.ToInt32(input17[i]) - 48;
                sum += cur * Weight[i];
            }
            return VCode[(sum % 11)];
        }
        static int GetAge(string input4)//算出年齡  
        {
            try
            {
                return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(input4);
            }
            catch { return 0; }
        }
        static bool CheckACode(string input2)//檢查地區碼  
        {
            if (Address.IndexOf(input2) != -1)
                return true;
            return false;
        }

    }

    public class IdCardUserInfo
    {
        /// <summary>
        /// 身份證號碼
        /// </summary>
        public string IdCardNum { get; set; }
        /// <summary>
        /// 性別 0男1女
        /// </summary>
        public int Gender { get; set; }
        /// <summary>
        /// 生日
        /// </summary>
        public DateTime BirthDay { get; set; }
        /// <summary>
        /// 年齡
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 身份證是否合法
        /// </summary>
        public bool IsValid { get; set; }
        /// <summary>
        /// 是不是成年人
        /// </summary>
        public bool IsAdult { get; set; }
    }
相關文章
相關標籤/搜索