首页 > 开发 > 综合 > 正文

身份证格式验证(含15位转换到18位)C#

2024-07-21 02:25:55
字体:
来源:转载
供稿:网友

using system;
using system.collections.generic;
using system.text;

namespace consoleapplication1
{
    class program
    {
        static void main(string[] args)
        {
            for (int i = 1; i < 10; i++)
            {
                console.write("{0}:    ", i);
                for (int k = 1; k <= i; k++)
                {
                    console.write("{0}" + "  ", i * k);
                }
              
              
                console.writeline();
            }
            string str = "620102197811196218";
            checkcid t = new checkcid();
            string str1 = t.checkcidinfo(str);
            console.write(str1);
        }

        /// <summary>
        /// 15 to 19
        /// </summary>
        public class idcard
        {
            // wi =2(n-1)(mod 11)
            int[] wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            // verify digit
            int[] vi = new int[] { 1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2 };
            private int[] ai = new int[18];
            public idcard()
            {
            }
            //verify
            public string verify(string idcard)
            {
                string strcard = "";
                if (idcard.length == 15)
                {
                    strcard = uptoeighteen(idcard);
                }
                if (idcard.length == 18)
                {
                    strcard = idcard;
                }
                //string verify = idcard.substring(17, 18);
                //if (verify.equals(getverify(idcard))) {
                //return true;
                //}
                return strcard;
            }
            //get verify
            public string getverify(string eightcardid)
            {
                int remaining = 0;
                if (eightcardid.length == 18)
                {
                    eightcardid = eightcardid.substring(0, 17);
                }
                if (eightcardid.length == 17)
                {
                    int sum = 0;
                    for (int i = 0; i < 17; i++)
                    {
                        string k = eightcardid.substring(i, 1);
                        ai[i] = int.parse(k);
                    }
                    for (int i = 0; i < 17; i++)
                    {
                        sum = sum + wi[i] * ai[i];
                    }
                    remaining = sum % 11;
                }
                return remaining == 2 ? "x" : (vi[remaining]).tostring();
            }
            //15 update to 18
            public string uptoeighteen(string fifteencardid)
            {
                string eightcardid = fifteencardid.substring(0, 6);
                eightcardid = eightcardid + "19";
                eightcardid = eightcardid + fifteencardid.substring(6, 9);
                eightcardid = eightcardid + getverify(eightcardid);
                return eightcardid;
            }
        }
        /// <summary>
        /// 检测
        /// </summary>
        public class checkcid
        {
        public 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);

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

        }
        }
    }
}
 

 



收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表