http://www.jaron.cn/chs_scripts/52/2005-11/20051102110621-101801.html

解决方案 »

  1.   

    从我的代码库里翻了一份出来,给出的是几个Helper方法,只支持对18位身份证的验证:    /// <summary>
        /// GB 11643-1999 Chinese ID number checker, simply only support 18bit ID.
        /// </summary>
        static class ChineseIDChecker
        {
            /// <summary>
            ///   Represent the special address code
            /// </summary>
            private static int[] addressCodes = { 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 };        /// <summary>
            /// ISO 7064:1983, MOD 11-2 Code Checking System
            /// </summary>
            private static int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };        /// <summary>
            /// ISO 7064:1983, MOD 11-2 Code Checking System Allowed check number.
            /// </summary>
            private static string[] AllowedCheckCodes = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };        private const int ADDRESSCODELENGHT = 6;
            private const int BIRTHDAYCODELENGHT = 8;
            private const int SERIALCODELENGTH = 3;
            private const int VALIDLENGTH = 18;        /// <summary>
            /// Validate Chinese ID number
            /// </summary>
            /// <param name="value">ID number</param>
            /// <returns>CheckResult enum</returns>
            static public CheckResult CheckIdentityCode(string value)
            {
                int strLen = value.Length;            if (strLen >= ADDRESSCODELENGHT)
                {
                    if (!CheckAddressCode(value.Substring(0, ADDRESSCODELENGHT)))
                    {
                        return CheckResult.ADD_CODE_ERROR;
                    }
                }            if (strLen >= ADDRESSCODELENGHT + BIRTHDAYCODELENGHT)
                {
                    if (!CheckBirthDayCode(value.Substring(ADDRESSCODELENGHT, BIRTHDAYCODELENGHT)))
                    {
                        return CheckResult.BIRTH_CODE_ERROR;
                    }
                }            if (strLen >= ADDRESSCODELENGHT + BIRTHDAYCODELENGHT + SERIALCODELENGTH)
                {
                    string serial = value.Substring(ADDRESSCODELENGHT + BIRTHDAYCODELENGHT, SERIALCODELENGTH);
                    int iSerial;
                    if (Int32.TryParse(serial, out iSerial))
                    {
                        if (iSerial == 0)
                        {
                            return CheckResult.SERIAL_ERROR;
                        }
                    }
                    else
                    {
                        return CheckResult.SERIAL_ERROR;
                    }
                }            if (strLen == VALIDLENGTH)
                {
                    if (!CheckSum(value))
                    {
                        return CheckResult.CHECK_CODE_ERROR;
                    }
                    else
                    {
                        return CheckResult.ID_OK;
                    }
                }            return CheckResult.LENGTH_ERROR;
            }        /// <summary>
            /// Check address.
            /// </summary>
            /// <param name="addr_value">Address value:string</param>
            /// <returns>result:bool</returns>
            static private bool CheckAddressCode(string addr_value)
            {
                int value;            //addr_value = addr_value.Substring(0, 2);            if (!Int32.TryParse(addr_value, out value))
                {
                    return false;
                }            value /= 10000;            int end = addressCodes.Length - 1;
                int start = 0;
                int mid = (end - start) / 2;            while (end >= start)
                {
                    if (value > addressCodes[mid])
                    {
                        start = mid + 1;
                    }
                    else if (value < addressCodes[mid])
                    {
                        end = mid - 1;
                    }
                    else
                    {
                        return true;
                    }
                    mid = (end - start) / 2 + start;
                }
                return false;
            }        /// <summary>
            /// Check birth code.
            /// </summary>
            /// <param name="value">Brith value:string</param>
            /// <returns>result:bool</returns>
            static private bool CheckBirthDayCode(string value)
            {
                DateTime tmp;
                string birth = value.Substring(0, 4) + "-" + value.Substring(4, 2) + "-" + value.Substring(6, 2);
                if (DateTime.TryParse(birth, out tmp))
                {
                    DateTime lowbound = new DateTime(1870, 1, 1);
                    DateTime upperbound = new DateTime(2010, 12, 31);
                    if (tmp < lowbound || tmp > upperbound)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }        /// <summary>
            /// Check validation code.
            /// </summary>
            /// <param name="value">Complete ID number:string</param>
            /// <returns>result:bool</returns>
            static private bool CheckSum(string value)
            {
                string check = value.Substring(17, 1);
                char[] Ai = value.ToCharArray();            int val = 0;            for (int i = 0; i < 17; i++)
                {
                    val += Wi[i] * (Ai[i] - '0');
                }
                if (check.ToLower() != AllowedCheckCodes[val % 11])
                {
                    return false;
                }            return true;
            }
        }
      

  2.   

    18位身份证,多加一个参考:public string[] PROVINCE_CODE = {
        null, null, null, null, null, null, null, null, null, null, null,
        "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null,
        "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null,
        "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null,
        "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null,
        "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null,
        "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null,
        "台湾", null, null, null, null, null, null, null, null, null,
        "香港", "澳门", null, null, null, null, null, null, null, null,
        "国外"};public bool CheckCidInfo(string ACidCode)
    {
        if (string.IsNullOrEmpty(ACidCode)) return false; // 空
        for (int i = 0; i < 17; i++)
            if ("0123456789".IndexOf(ACidCode[i]) < 0) return false; // 前17位必须数字
        if ("0123456789Xx".IndexOf(ACidCode[17]) < 0) return false; // 最后一位必须数字或X
        int vProvince = int.Parse(ACidCode.Substring(0, 2));
        if (vProvince > PROVINCE_CODE.Length ||
            string.IsNullOrEmpty(PROVINCE_CODE[vProvince])) return false; // 地址码错误
        DateTime vBirthday;
        if (!DateTime.TryParseExact(ACidCode.Substring(6, 8), "yyyyMMdd", null,
            System.Globalization.DateTimeStyles.None, out vBirthday)) return false; // 日期格式错误
        if (vBirthday > DateTime.Now) return false; // 未来的人???
        if (vBirthday < new DateTime(1900, 10, 01)) return false; // 还没建国
        int T = 0;
        for (int i = 0; i < 18; i++)
        {
            int j = "xX".IndexOf(ACidCode[i]) < 0 ? ACidCode[i] - '0' : 10;
            T += (int)Math.Pow(2, 17 - i) % 11 * j;
        }
        if (T % 11 != 1) return false; // 验证码错误
        return true;
    }
      

  3.   

    两种方法:
    一,正则表达式
    二,逐个字符判断
    string UserNumber=this.textbox1.text.tostring();
    foreach(char ch in UserNumber){...}
      

  4.   

    TO hdt:不管是在WinForm还是WebForm里对身份证号的有效性进行验证,都只是一种初期的验证,它的确不能反应出其真实性,但是从我们程序员的角度来说,这种初期的验证可以在很在程度上避免使用者的误操作导致的不可预计的后果。我想你在开发的过程中也会对用户的输入进行验证吧,要不然ASP.NET提供这么多的验证控件不是吃饱了撑的!
    再说了,就算是通过公安系统调出资料也不一定可以确认持证人的真实性,所以要说有没有意义这种话实在是对不起你的4颗星啊。