Unity 身份证本地验证,识别

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text.RegularExpressions;
using UnityEngine.UI;

public class RealNameAuthentication : MonoBehaviour
{
    [SerializeField] private InputField inputField_1;
    [SerializeField] private InputField inputField_2;
    [SerializeField] private InputField inputField_3;
    public void InputFieldParseIdCard()
    {
        ParseIdCard(inputField_1.text);
    }
    public void InputFieldCheckIdCard()
    {
        Debug.Log(CheckIdCard(inputField_2.text));
    }
    public void InputFieldCheckTwID()
    {
        Debug.Log( CheckTwID(inputField_3.text));
    }

    // 输入全部身份证号码,计算出出生年月日以及性别和年龄
    private void ParseIdCard(string val)
    {
        string birthdayValue = "19931221";
        string sexId = "";
        string sexText = "";

        if (15 == val.Length) //15位身份证号码
        {
            birthdayValue = val[6].ToString() + val[7].ToString();
            if (int.Parse(birthdayValue) < 10)
            {
                birthdayValue = "20" + birthdayValue;
            }
            else
            {
                birthdayValue = "19" + birthdayValue;
            }

            birthdayValue = birthdayValue + '-' + val[8] + val[9] + '-' + val[10] + val[11];




            if (int.Parse(((val[14] / 2) * 2).ToString()) != val[14])
            {
                sexId = "1";
                sexText = "男";
            }
            else
            {
                sexId = "2";
                sexText = "女";
            }
        }

        if (18 == val.Length)
        {
            //18位身份证号码
            birthdayValue = val[6].ToString() + val[7] + val[8] + val[9] + '-' + val[10] + val[11] + '-' + val[12] +
                            val[13];
            if (int.Parse((val[16] / 2).ToString()) * 2 != val[16])
            {
                sexId = "1";
                sexText = "男";
            }
            else
            {
                sexId = "2";
                sexText = "女";
            }
        }

        //年龄
        birthdayValue = birthdayValue.Replace("-", "/");
        string[] YMD = birthdayValue.Split('/');
        DateTime dt1 = new DateTime(int.Parse(YMD[0]), int.Parse(YMD[1]), int.Parse(YMD[2]));
        DateTime dt2 = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
        int age = dt2.Year - dt1.Year;
        var m = dt2.Month - dt1.Month;
        if (m < 0)
            age--;
        Debug.Log(dt1.Year);
        Debug.Log(dt2.Year);
        // 返回 2017/12/21  "1"  "男"  29
        Debug.Log($"{birthdayValue}, {sexId}, {sexText}, {age}");
        //return new Array(birthdayValue, sexId, sexText, age);
    }


    // 验证身份证号
    private string CheckIdCard(string idcard)
    {
        idcard = idcard.ToUpper();
        string[] Errors = new string[]
        {
            "校验成功!", "身份证号码位数不对!",
            "身份证号码出生日期超出范围或含有非法字符!", "身份证号码校验错误!",
            "身份证号码中地区编码不正确!"
        };

        Dictionary<int, string> area = new Dictionary<int, string>
        {
            {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, "国外"}
        };



        string M, JYM;
        double S, Y;

        if (idcard.Length != 15 && idcard.Length != 18) return Errors[1];
        if (idcard.Substring(0, 6) == "000000" || idcard.Substring(0, 6) == "111111" ||
            idcard.Substring(0, 6) == "222222" || idcard.Substring(0, 6) == "333333" ||
            idcard.Substring(0, 6) == "444444" || idcard.Substring(0, 6) == "555555" ||
            idcard.Substring(0, 6) == "666666" || idcard.Substring(0, 6) == "777777" ||
            idcard.Substring(0, 6) == "888888" || idcard.Substring(0, 6) == "999999") return Errors[4];
        if (idcard.Substring(0, 6) == "123456" || idcard.Substring(0, 6) == "234567" ||
            idcard.Substring(0, 6) == "345678" || idcard.Substring(0, 6) == "456789" ||
            idcard.Substring(0, 6) == "567890" || idcard.Substring(0, 6) == "012345" ||
            idcard.Substring(0, 6) == "543210" || idcard.Substring(0, 6) == "432109" ||
            idcard.Substring(0, 6) == "321098" || idcard.Substring(0, 6) == "210987" ||
            idcard.Substring(0, 6) == "109876" || idcard.Substring(0, 6) == "098765" ||
            idcard.Substring(0, 6) == "987654" || idcard.Substring(0, 6) == "876543" ||
            idcard.Substring(0, 6) == "765432") return Errors[4];
        if (idcard.Substring(0, 6) == "121212" || idcard.Substring(0, 6) == "131313" ||
            idcard.Substring(0, 6) == "141414" || idcard.Substring(0, 6) == "151515" ||
            idcard.Substring(0, 6) == "161616" || idcard.Substring(0, 6) == "171717" ||
            idcard.Substring(0, 6) == "181818" || idcard.Substring(0, 6) == "191919" ||
            idcard.Substring(0, 6) == "101010") return Errors[4];
        if (idcard.Substring(0, 6) == "212121" || idcard.Substring(0, 6) == "232323" ||
            idcard.Substring(0, 6) == "242424" || idcard.Substring(0, 6) == "252525" ||
            idcard.Substring(0, 6) == "262626" || idcard.Substring(0, 6) == "272727" ||
            idcard.Substring(0, 6) == "282828" || idcard.Substring(0, 6) == "292929" ||
            idcard.Substring(0, 6) == "202020") return Errors[4];
        if (idcard.Substring(0, 6) == "313131" || idcard.Substring(0, 6) == "323232" ||
            idcard.Substring(0, 6) == "343434" || idcard.Substring(0, 6) == "353535" ||
            idcard.Substring(0, 6) == "363636" || idcard.Substring(0, 6) == "373737" ||
            idcard.Substring(0, 6) == "383838" || idcard.Substring(0, 6) == "393939" ||
            idcard.Substring(0, 6) == "303030") return Errors[4];
        if (idcard.Substring(0, 6) == "414141" || idcard.Substring(0, 6) == "424242" ||
            idcard.Substring(0, 6) == "434343" || idcard.Substring(0, 6) == "454545" ||
            idcard.Substring(0, 6) == "464646" || idcard.Substring(0, 6) == "474747" ||
            idcard.Substring(0, 6) == "484848" || idcard.Substring(0, 6) == "494949" ||
            idcard.Substring(0, 6) == "404040") return Errors[4];
        if (idcard.Substring(0, 6) == "515151" || idcard.Substring(0, 6) == "525252" ||
            idcard.Substring(0, 6) == "535353" || idcard.Substring(0, 6) == "545454" ||
            idcard.Substring(0, 6) == "565656" || idcard.Substring(0, 6) == "575757" ||
            idcard.Substring(0, 6) == "585858" || idcard.Substring(0, 6) == "595959" ||
            idcard.Substring(0, 6) == "505050") return Errors[4];
        if (idcard.Substring(0, 6) == "616161" || idcard.Substring(0, 6) == "626262" ||
            idcard.Substring(0, 6) == "636363" || idcard.Substring(0, 6) == "646464" ||
            idcard.Substring(0, 6) == "656565" || idcard.Substring(0, 6) == "676767" ||
            idcard.Substring(0, 6) == "686868" || idcard.Substring(0, 6) == "696969" ||
            idcard.Substring(0, 6) == "606060") return Errors[4];
        if (idcard.Substring(0, 6) == "717171" || idcard.Substring(0, 6) == "727272" ||
            idcard.Substring(0, 6) == "737373" || idcard.Substring(0, 6) == "747474" ||
            idcard.Substring(0, 6) == "757575" || idcard.Substring(0, 6) == "767676" ||
            idcard.Substring(0, 6) == "787878" || idcard.Substring(0, 6) == "797979" ||
            idcard.Substring(0, 6) == "707070") return Errors[4];
        if (idcard.Substring(0, 6) == "818181" || idcard.Substring(0, 6) == "828282" ||
            idcard.Substring(0, 6) == "838383" || idcard.Substring(0, 6) == "848484" ||
            idcard.Substring(0, 6) == "858585" || idcard.Substring(0, 6) == "868686" ||
            idcard.Substring(0, 6) == "878787" || idcard.Substring(0, 6) == "898989" ||
            idcard.Substring(0, 6) == "808080") return Errors[4];
        if (idcard.Substring(0, 6) == "919191" || idcard.Substring(0, 6) == "929292" ||
            idcard.Substring(0, 6) == "939393" || idcard.Substring(0, 6) == "949494" ||
            idcard.Substring(0, 6) == "959595" || idcard.Substring(0, 6) == "969696" ||
            idcard.Substring(0, 6) == "979797" || idcard.Substring(0, 6) == "989898" ||
            idcard.Substring(0, 6) == "909090") return Errors[4];
        if (area[int.Parse(idcard.Substring(0, 2))] == null) return Errors[4];

        string ereg;
        switch (idcard.Length)
        {
            case 15:
                if ((int.Parse(idcard.Substring(6, 2)) + 1900) % 4 == 0 ||
                    ((int.Parse(idcard.Substring(6, 2)) + 1900) % 100 == 0 &&
                     (int.Parse(idcard.Substring(6, 2)) + 1900) % 4 == 0))
                {
                    ereg =
                        @"^[1-9] [0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2] [0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2] [0-9]|30)|02(0[1-9]|[1-2] [0-9]))[0-9]{3}$"; //测试出生日期的合法性
                }
                else
                {
                    ereg =
                        @"^[1-9] [0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2] [0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2] [0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"; //测试出生日期的合法性
                }

                if (!Regex.IsMatch(idcard, ereg))
                    return Errors[0];
                else
                    return Errors[2];
            case 18:
                if (int.Parse(idcard.Substring(6, 4)) % 4 == 0 || (int.Parse(idcard.Substring(6, 4)) % 100 == 0 &&
                                                                   int.Parse(idcard.Substring(6, 4)) % 4 == 0))
                {
                    ereg =
                        @"/^[1 - 9][0 - 9]{ 5}19[0 - 9]{ 2} ((01 | 03 | 05 | 07 | 08 | 10 | 12)(0[1 - 9] |[1 - 2][0 - 9] | 3[0 - 1]) | (04 | 06 | 09 | 11)(0[1 - 9] |[1 - 2][0 - 9] | 30) | 02(0[1 - 9] |[1 - 2][0 - 9]))[0 - 9]{ 3}[0-9Xx]$/"; //闰年出生日期的合法性正则表达式
                }
                else
                {
                    ereg =
                        @"/^[1-9] [0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2] [0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2] [0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/"; //平年出生日期的合法性正则表达式
                }

                if (!Regex.IsMatch(idcard, ereg))
                {
                    S = (double.Parse(idcard[0].ToString()) + double.Parse(idcard[10].ToString())) * 7 +
                        (double.Parse(idcard[1].ToString()) + double.Parse(idcard[11].ToString())) * 9 +
                        (double.Parse(idcard[2].ToString()) + double.Parse(idcard[12].ToString())) * 10 +
                        (double.Parse(idcard[3].ToString()) + double.Parse(idcard[13].ToString())) * 5 +
                        (double.Parse(idcard[4].ToString()) + double.Parse(idcard[14].ToString())) * 8 +
                        (double.Parse(idcard[5].ToString()) + double.Parse(idcard[15].ToString())) * 4 +
                        (double.Parse(idcard[6].ToString()) + double.Parse(idcard[16].ToString())) * 2 +
                        double.Parse(idcard[7].ToString()) * 1 + double.Parse(idcard[8].ToString()) * 6 +
                        double.Parse(idcard[9].ToString()) * 3;
                    Y = S % 11;
                    M = "F";
                    JYM = "10X98765432";
                    M = JYM.Substring((int) Y, 1);
                    if (M == idcard[17].ToString())
                        return Errors[0];
                    else
                        return Errors[3];
                }
                else
                {
                    return Errors[2];
                }

            default:
                return Errors[1];
        }
    }

    // 检查身份证格式
    private string CheckTwID(string id)
    {
        /*//建立字母分数数组(A~Z)
        int[] citys = new int[]
        {
            1, 10, 19, 28, 37, 46, 55, 64, 39, 73, 82, 2, 11,
            20, 48, 29, 38, 47, 56, 65, 74, 83, 21, 3, 12, 30
        };
        id = id.ToUpper();

        // 使用「正规表达式」检验格式
        if (!Regex.IsMatch(id, @"/^[A - Z](1 | 2)\d{8}$/ i"))
        {
            return "身份证号码格式不对!";
        }
        else
        {
            //计算总分
            //C#中用  Convert.ToByte(A[i])  就跟 as语言或js语言中 .charCodeAt() 输出结果是一样的
            var total = citys[Convert.ToByte(id[0]) - 65];
            for (var i = 1; i <= 8; i++)
            {
                total += ((id[i]) * (9 - i));
            }

            //补上检查码(最后一码)
            total += id[9];
            //检查比对码(余数应为0); 
            if (total % 10 == 0)
            {
                return "true";
            }
            else
            {
                return "身份证号码格式不对!";
            }
        }*/
        string pattern = @"^\d{17}(?:\d|X)$";
        string birth = id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
        DateTime time = new DateTime();
 
        int[] arr_weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };     // 加权数组
        string[] id_last = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };   // 校验数组
        int sum = 0;
        for (int i = 0; i < 17; i++)
        {
            sum += arr_weight[i] * int.Parse(id[i].ToString());
        }
        int result = sum % 11;  // 实际校验位的值
 
        if (Regex.IsMatch(id, pattern))                     // 18位格式检查
        {
            if(DateTime.TryParse(birth, out time))          // 出生日期检查
            {
                if (id_last[result] == id[17].ToString())   // 校验位检查
                {
                   return ("身份证号格式正确!");
                }
                else
                {
                    return("最后一位校验错误!");
                }
            }
            else
            {
                return("出生日期验证失败!");
            }
        }
        else
        {
            return("身份证号格式错误!");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

俊24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值