C#.NET身份证验证算法
獲取源碼方式:
第一種:打開(kāi)微信,搜一搜"別打我女兒的主意"打開(kāi)微信小程序,找到菜單欄,點(diǎn)擊源碼,進(jìn)去就可以獲得鏈接
第二種:可以給本文點(diǎn)贊、好評(píng),然后發(fā)郵件到792166417@qq.com,如果有學(xué)習(xí)資料視頻可以分享的話,可以捎帶分享給我。
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("失敗");
 ? ? ? ? ? ? }
 ? ? ? ? }
 ? ? ? ? //加權(quán)因子
 ? ? ? ? private static readonly int[] _factors = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
 ? ? ? ? //驗(yàn)證位置
 ? ? ? ? private static readonly int[] _codes = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
 ? ? ? ? /// <summary>
 ? ? ? ? /// 驗(yàn)證是否為身份證號(hào)
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="idcard">身份證號(hào)</param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? public static bool Check(string idcard)
 ? ? ? ? {
 ? ? ? ? ? ? if (idcard.Length == 18)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? if (ValidBirthday(idcard) && ValidateCode(idcard))
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? return true;
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return false;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 驗(yàn)證生日
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="idcard">身份證號(hào)</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>
 ? ? ? ? /// 驗(yàn)證身份證規(guī)則
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="idcard">身份證號(hào)</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]);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? //獲取驗(yàn)證位置
 ? ? ? ? ? ? int position = sum % 11;
 ? ? ? ? ? ? if (list[17].Equals(_codes[position].ToString()))
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? return true;
 ? ? ? ? ? ? }
 ? ? ? ? ? ? return false;
 ? ? ? ? }
 ? ? }
 }
總結(jié)
以上是生活随笔為你收集整理的C#.NET身份证验证算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: 【机器学习】feature_import
 - 下一篇: 解决-ubuntu 安装redis无法启