当前位置: 移动技术网 > IT编程>开发语言>c# > C#身份证验证小例子

C#身份证验证小例子

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: private string checkcidinfo(string cid)     &

复制代码 代码如下:

 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;
            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);
            }
            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 ? "男" : "女"));

        }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网