如何验证身份证所属地区(要求精确到区),谢谢!

解决方案 »

  1.   

    下面那个地址是国家统计局的网站你可以去看看
    http://www.stats.gov.cn/tjbz/xzqhdm/t20070411_402397928.htm身份证的前6位是代表你是什么地方的,他是精确到县一级的..自己整理一下就可以查询了
      

  2.   

     private string CheckCidInfo(string cid)
        {
            string[] aCity = new string[] { 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, "国外" };
            double iSum = 0;
            string info = "";
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
            System.Text.RegularExpressions.Match mc = rg.Match(cid);
            if (!mc.Success)
            {
                return "你输入的号码有误!";
            }
            cid = cid.ToLower();
            cid = cid.Replace("x", "a");
            if (aCity[int.Parse(cid.Substring(0, 2))] == null)
            {
                return "非法地区";
            }
            try
            {
                DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
            }
            catch
            {
                return "非法生日";
            }
            for (int i = 17; i >= 0; i--)
            {
                iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);        }
            double intI = iSum % 11;
          //  Response.Write(intI.ToString());
            if (iSum % 11 != 1)
            {
                return ("非法证号");
            }
            //return(aCity[int.Parse(cid.Substring(0,2))]+","+cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2)+","+(int.Parse(cid.Substring(16,1))%2== 1)); 
            string strSex="";
            if (int.Parse(cid.Substring(16, 1)) % 2 == 1)
            {
                strSex = "男";
            }
            else
            {
                strSex = "女";
            }
            return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + strSex); 
        }    protected void Button1_Click(object sender, EventArgs e)
        {
           Response.Write(CheckCidInfo(this.TextBox1.Text.ToString()));
        }
    通过这就可以查询了。