身份证验证算法
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HT.Cache
{
public class IdCard
{
//加权因子
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;
}
}
}
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HT.Cache
{
public class IdCard
{
//加权因子
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;
}
}
}