国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

身份證格式驗證(含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?"男":"女"));

        }
        }
    }
}
 

 



收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城步| 年辖:市辖区| 瑞金市| 青川县| 祁东县| 广州市| 临西县| 沙田区| 乐至县| 仙游县| 屏东市| 清苑县| 鹿泉市| 岳西县| 西吉县| 师宗县| 巴中市| 巧家县| 东辽县| 咸宁市| 抚顺市| 龙海市| 阿图什市| 宁乡县| 平阴县| 西乌珠穆沁旗| 平顶山市| 通城县| 吕梁市| 寿宁县| 崇左市| 防城港市| 彭山县| 华坪县| 奉新县| 宁化县| 周宁县| 苏尼特左旗| 长丰县| 吐鲁番市| 余江县|