验证是不是合法的18位身份证号码代码

//验证是不是合法的18位身份证号码代码:
    public static bool IsValidIDNumber(string strId)
    {
        System.Text.RegularExpressions.Regex regId 
            = new 
            System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$"
        , System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        if (strId.Length != 18)
        {
            return false;
        }
        else
        {
            if (!regId.IsMatch(strId))
            {
                return false;
            }
            else
            {
                int iS = 0;
                int[] iW = new int[]
                {
                    7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2
                };
                string LastCode = "10X98765432";
                for (int i = 0; i < 17; i++)
                {
                    iS += int.Parse(strId.Substring(i, 1)) * iW[i];
                }
                int iY = iS % 11;
                if (strId.Substring(17, 1) != LastCode.Substring(iY, 1))
                    return false;
                return true;
            }
        }
    }
发布了115 篇原创文章 · 获赞 36 · 访问量 9903

猜你喜欢

转载自blog.csdn.net/weixin_44548307/article/details/102908921
今日推荐