C#检查身份证号码

代码摘自 http://topic.csdn.net/u/20101231/09/039bdd22-3f0a-4bbb-908e-d887b4d7bbfe.html?19933

public static class Util
{
    // <summary>
    /// 检查身份证信息是否合法
    /// 2010-10-13 jorln
    /// </summary>
    /// <param name="num">身份证号</param>
    /// <param name="sex">性别</param>
    /// <returns></returns>
    public static bool CheckIdentity(string num, string sex)
    {
        bool isIden = false;
        if (Convert.ToInt32(num.Substring(6, 4)) > 1850 && Convert.ToInt32(num.Substring(6, 4)) < 2010 && Convert.ToInt32(num.Substring(10, 2)) <= 12 && Convert.ToInt32(num.Substring(12, 2)) <= 31)
        {
            if (num.Substring(10, 2) == "02" && Convert.ToInt32(num.Substring(10, 2)) > 29)
                return isIden;
            if ((Convert.ToInt32(num.Substring(16, 1)) % 2 == 0 && sex == "女") || (Convert.ToInt32(num.Substring(16, 1)) % 2 != 0 && sex == "男"))
            {
                int count = 0;
                int[] iden = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, };
                for (int i = 0; i < iden.Length; i++)
                {
                    count += Convert.ToInt32(num.Substring(i, 1)) * iden[i];
                }
                int cc = count % 11;
                char num18 = cc == 0 ? '1' : (cc == 1 ? '2' : (cc == 2 ? 'X' : (cc == 3 ? '9' : (cc == 4 ? '8' : (cc == 5 ? '7' : (cc == 6 ? '6' : (cc == 7 ? '5' : (cc == 8 ? '4' : (cc == 9 ? '3' : '2')))))))));
                isIden = num.Substring(17, 1).ToUpper() == num18.ToString() ? true : false;

            }
        }
        return isIden;
    }
}

posted @ 2011-04-18 20:00  梦幻泡影  阅读(418)  评论(0编辑  收藏  举报