C#.NET身份证验证算法

获取源码方式:

第一种:打开微信,搜一搜"别打我女儿的主意"打开微信小程序,找到菜单栏,点击源码,进去就可以获得链接

第二种:可以给本文点赞好评,然后发邮件到792166417@qq.com,如果有学习资料视频可以分享的话,可以捎带分享给我。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IDcard
{
    public partial class Card : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Check("360601198801310373"))
            {
                Response.Write("成功");
            }
            else
            {
                Response.Write("失败");
            }
        }

        //加权因子
        private static readonly int[] _factors = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
        //验证位置
        private static readonly int[] _codes = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

        /// <summary>
        /// 验证是否为身份证号
        /// </summary>
        /// <param name="idcard">身份证号</param>
        /// <returns></returns>
        public static bool Check(string idcard)
        {
            if (idcard.Length == 18)
            {
                if (ValidBirthday(idcard) && ValidateCode(idcard))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 验证生日
        /// </summary>
        /// <param name="idcard">身份证号</param>
        /// <returns></returns>
        private static bool ValidBirthday(string idcard)
        {
            string year = idcard.Substring(6, 4);
            string month = idcard.Substring(10, 2);
            string day = idcard.Substring(12, 2);
            DateTime date;
            string xdate = year + month + day;
            bool result = DateTime.TryParseExact(xdate, "yyyyMMdd", new DateTimeFormatInfo(), DateTimeStyles.AdjustToUniversal, out date);
            if (!result)
            {
                return false;
            }
            string xmonth = date.Month < 10 ? "0" + date.Month : date.Month.ToString();
            string xday = date.Day < 10 ? "0" + date.Day : date.Day.ToString();
            if (!date.Year.ToString().Equals(year) || !month.Equals(xmonth) || !day.Equals(xday))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 验证身份证规则
        /// </summary>
        /// <param name="idcard">身份证号</param>
        /// <returns></returns>
        private static bool ValidateCode(string idcard)
        {
            int sum = 0;
            char[] chars = idcard.ToCharArray();
            List<string> list = chars.Select(c => c.ToString()).ToList();
            if (list[17] == "x")
            {
                list[17] = "10";
            }
            for (int i = 0; i < 17; i++)
            {
                sum += _factors[i] * Convert.ToInt32(list[i]);
            }
            //获取验证位置
            int position = sum % 11;
            if (list[17].Equals(_codes[position].ToString()))
            {
                return true;
            }
            return false;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值