private string[] m_AreaCodes =
        {
            "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"
        };
        private bool isValidIdCardNo(string idstr, out DateTime birthdate, out string errorMsg)
        {
            errorMsg = null;
            birthdate = DateTime.Now;
            //校验前两位是否是国内某个地区的代码
            if (Array.IndexOf(this.m_AreaCodes, idstr.Substring(0, 2)) < 0)
            {
                errorMsg = "您输入的身份证号码不正确(没有通过所属地区代码的验证)!";
                return false;
            }            bool isDate = false;
            if (idstr.Length == 18)
            {
                string checkcode = "10X98765432";
                //加权因子
                int[] factors = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                //计算加权和
                int sum = 0;
                for (int i = 0; i < 17; i++)
                {
                    sum += (idstr[i] - '0') * factors[i];  //48是‘0’的ascii码
                }
                char c = checkcode[(sum % 11)];
                if (idstr[17] != c)
                {
                    errorMsg = "身份证校验码错误!";
                    return false;
                }
                //校验一下用户输入的日期段是否是真正的日期
                isDate = DateTime.TryParse(string.Format("{0}{1}{2}{3}-{4}{5}-{6}{7}",
                    idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11], idstr[12], idstr[13]), out birthdate);
            }
            else if (idstr.Length == 15)
            {
                //校验一下用户输入的日期段是否是真正的日期
                isDate = DateTime.TryParse(string.Format("19{0}{1}-{2}{3}-{4}{5}",
                    idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11]), out birthdate);            }
            else
            {
                errorMsg = "身份证位数不正确(应为15位或者18位)!";
                return false;
            }
            if (!isDate)
            {
                errorMsg = "身份证号码的出生日期部分不正确!";
                return false;
            }
            return true;
        }这是一个写好的方法,目的是判断身份证的真假.窗体有一个textbox,一个button,就是不会用.不会调用呀.现在想来自已是一笨驴...
各位大侠...教教这头驴吧.

解决方案 »

  1.   

    DateTime birthdate;
    string errorMsg;
    string idstr="12345667889899";
    if(isValidIdCardNo(idstr,out birthdate,outerrorMsg)
    {
    .....
    }
    else
    {
    .....
    }
      

  2.   

    双击Button在方法中写入:
    string identifierStr=textBox.Text;
    //随便给定一个日期,这里的xxx,yy,zz为整数
    DateTime dt;
    string error;isValidIdCardNo(identifierStr,dt,out error);
    、、
    这样就可以调用此函数了!
      

  3.   

    DateTime dt;
    isValidIdCardNo("",out dt,out str)
    正则判断身份证
    public static string CheckIDCard(string pId)
    {
    string[] arrVerifyCode = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
    int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
    int[] Checker = { 1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1 };
    if (pId.Length != 15 && pId.Length != 18) throw new FormatException("身份证号码只有15位或18位");
    string Ai = pId.Length == 18 ? pId.Substring(0, 17) : pId.Substring(0, 6) + "19" + pId.Substring(6, 16);
    if (!Regex.IsMatch(Ai, @"^\d{17}")) throw new FormatException("身份证除最后一位外,必须为数字!");
    int year = int.Parse(Ai.Substring(6, 4));
    DateTime birthday = new DateTime(year, int.Parse(Ai.Substring(10, 2)), int.Parse(Ai.Substring(12, 2)));
    int ret = 0;
    for (int i = 0; i < 17; i++) ret += int.Parse(Ai.Substring(i, 1)) * Wi[i];
    Ai += arrVerifyCode[ret %= 11];
    return pId.Length == 18 && pId.ToLower() != Ai ? "身份证输入错误!" : Ai;
    }
      

  4.   

    to: 
    wuyq11更复杂了,更不会用了.
    to:
    kensouterry可否再说详细点;
    to:
    soaringbird
    可否再说详细点;
      

  5.   

            /// <summary>
            /// button的单击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime birthdate; 
                string errorMsg; 
                string idstr="511533242352355"; //这里是需要证实的身份证号号码,你自己定义
                if(isValidIdCardNo(idstr,out birthdate,outerrorMsg))//在这个if语句里,调用方法
                {
                    MessageBox .Show ("身份证是真的");
                }
                else 
                {
                    MessageBox .Show ("身份证是假的");
                }
            }
    这样你应该明白了 ,你只需要单击你from里的button,里面就可以是上面的代码
      

  6.   

    DateTime birthdate;
    string errorMsg;
    string idstr="12345667889899";
    if(isValidIdCardNo(idstr,out birthdate,out errorMsg)
    {
     //是真的
      Console.WriteLine("此人的生日是:{0}",birthdate);
    }
    else
    {
      //是假的
      Console.WriteLine("此为假的证,错误信息:{0}",errorMsg);
      

  7.   

            /// <summary>
            /// button的单击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime birthdate; 
                string errorMsg; 
                string idstr="511533242352355"; //这里是需要证实的身份证号号码,你自己定义
                if(isValidIdCardNo(idstr,out birthdate,out errorMsg))//在这个if语句里,调用方法
                {
                    MessageBox .Show ("身份证是真的");
                }
                else 
                {
                    MessageBox .Show ("身份证是假的");
                }
            }
    修改一下