18位身份证验证代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace project.admin.dataclass
{

    /// <summary>
    /// 身份证识别类,有效年份:1900年~2999年
    /// </summary>
    public class pid
    {

        #region 类中私有变量
        private int S = 0, Ai, Y;
        private int[] Wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
        private char[] YY = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

        //地区号
        private string addrnum = "";
        //出生年
        private string birthyear = "";
        //出生月
        private string birthmonth = "";
        //出生日
        private string birthday = "";
        //顺序号
        private string ranknum = "";
        //性别号
        private string sexnum = "";
        //校验号
        private string checkcode = "";
        //省份
        private string province = "";
        //地级市
        private string provincecity = "";
        //县级市
        private string countrycity = "";
        #endregion

        #region 类属性
        /// <summary>
        /// 出生年
        /// </summary>
        public string BirthYear { get { return birthyear; } }
        /// <summary>
        /// 出生月
        /// </summary>
        public string BirthMonth { get { return birthmonth; } }
        /// <summary>
        /// 出生日
        /// </summary>
        public string BirthDay { get { return birthday; } }
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get { return sexnum; } }
        /// <summary>
        /// 省份
        /// </summary>
        public string Province { get { return province; } }
        /// <summary>
        /// 地级市
        /// </summary>
        public string ProvinceCity { get { return provincecity; } }
        /// <summary>
        /// 县级市
        /// </summary>
        public string CountryCity { get { return countrycity; } }
        #endregion

        /// <summary>
        /// 判断身份证是否正确
        /// </summary>
        /// <param name="iid">身份证号码</param>
        /// <returns></returns>
        public bool CheckId(string iid)
        {
            //长度验证
            if (iid.Length != 15 && iid.Length != 18)
                return false;

            //15位升18位
            if (iid.Length == 15)
            {
                try
                {
                    iid = Change15To18(iid);
                }
                catch
                {
                    return false;
                }
            }

            //地区号
            addrnum = iid.Substring(0, 6);
            //出生年
            birthyear = iid.Substring(6, 4);
            //出生月
            birthmonth = iid.Substring(10, 2);
            //出生日
            birthday = iid.Substring(12, 2);
            //顺序号
            ranknum = iid.Substring(14, 2);
            //性别号
            sexnum = iid.Substring(16, 1);
            //校验号
            checkcode = iid.Substring(17, 1);
            //省份
            province = "";
            //地级市
            provincecity = "";
            //县级市
            countrycity = "";

            #region 取值范围验证
            if (Convert.ToInt32(addrnum) < 110000 || Convert.ToInt32(addrnum) > 818888)
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }
            if (Convert.ToInt32(birthyear) < 1900 || Convert.ToInt32(birthyear) > 2999)
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }
            if (Convert.ToInt32(birthmonth) < 1 || Convert.ToInt32(birthmonth) > 12)
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }
            if (Convert.ToInt32(birthday) < 1 || Convert.ToInt32(birthday) > 31)
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }
            #endregion

            #region 校验码验证
            if (iid.Length == 18)
            {
                for (int i = 0; i <= 16; i++)
                {
                    Ai = Convert.ToInt32(iid.Substring(i, 1));
                    S += Ai * Convert.ToInt32(Wi[i]);
                }
                Y = S % 11;
                if (YY[Y].ToString().ToLower() != iid.Substring(17, 1).ToLower())
                {
                    birthyear = "";
                    birthmonth = "";
                    birthday = "";
                    sexnum = "";
                    province = "";
                    provincecity = "";
                    countrycity = "";
                    return false;
                }
            }
            else
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }
            #endregion

            #region 所属地区验证
            /*
            string sqlstr = "select [ID],[BM],[DQ] from [IDC] where [BM]=@BM";
            SqlParameter[] param = new SqlParameter[1];
            DataTable dt = new DataTable();

            try
            {
                //查找所属省
                param[0] = DAL.SqlHelper.GetParameter("@BM", SqlDbType.Int, 4, "[BM]", Convert.ToInt32(addrnum.Substring(0, 2) + "0000"));
                dt = DAL.SqlHelper.GetDataTable(sqlstr, param);
                if (dt.Rows.Count > 0)
                {
                    province = dt.Rows[0]["DQ"].ToString();
                }
                else
                {
                    birthyear = "";
                    birthmonth = "";
                    birthday = "";
                    sexnum = "";
                    province = "";
                    provincecity = "";
                    countrycity = "";
                    return false;
                }

                //查找所属地级市
                param[0] = DAL.SqlHelper.GetParameter("@BM", SqlDbType.Int, 4, "[BM]", Convert.ToInt32(addrnum.Substring(0, 4) + "00"));
                dt = DAL.SqlHelper.GetDataTable(sqlstr, param);
                if (dt.Rows.Count > 0)
                {
                    provincecity = dt.Rows[0]["DQ"].ToString().Remove(0, province.Length);
                }
                else
                {
                    birthyear = "";
                    birthmonth = "";
                    birthday = "";
                    sexnum = "";
                    province = "";
                    provincecity = "";
                    countrycity = "";
                    return false;
                }

                //查找所属县级市
                param[0] = DAL.SqlHelper.GetParameter("@BM", SqlDbType.Int, 4, "[BM]", Convert.ToInt32(addrnum));
                dt = DAL.SqlHelper.GetDataTable(sqlstr, param);
                if (dt.Rows.Count > 0)
                {
                    countrycity = dt.Rows[0]["DQ"].ToString().Remove(0, province.Length + provincecity.Length);
                }
                else
                {
                    countrycity = "";
                }
            }
            catch
            {
                birthyear = "";
                birthmonth = "";
                birthday = "";
                sexnum = "";
                province = "";
                provincecity = "";
                countrycity = "";
                return false;
            }*/
            #endregion

            //性别字符换
            if (Convert.ToInt32(sexnum) % 2 > 0)
                sexnum = "男";
            else
                sexnum = "女";

            return true;
        }

        /// <summary>
        /// 15位升18位身份证
        /// </summary>
        /// <param name="OldId">老的15位身份证</param>
        /// <returns></returns>
        private string Change15To18(string OldId)
        {
            //加入19年份
            OldId = OldId.Insert(6, "19");
            //加入最后一位校验码
            int i = 0;
            int num = 0;
            string code = "";
            for (i = 18; i >= 2; i--)
            {
                num += (Convert.ToInt32(Math.Pow(2, (i - 1)) % 11)) * Convert.ToInt32(OldId.Substring(18 - i, 1));
            }
            num = num % 11;
            switch (num)
            {
                case 0: code = "1"; break;
                case 1: code = "0"; break;
                case 2: code = "X"; break;
                default: code = (12 - num).ToString().Trim(); break;
            }
            return OldId + code;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值