自己用的身份证号验证函数
/// <summary>
/// 验证身份证
/// 1.校验生日
/// 2.校验最后一位(校验和)
/// </summary>
/// <param name="txt">ID</param>
/// <returns>结果</returns>
public static bool RegexIDCared(string txt)
{
bool res = new Regex(@"^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$").IsMatch(txt);
if (res)
{
res = false;
// 校验表
int[] check = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char[] checkSum = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
// 校验源
string checkSource = txt.Substring(0, 17);
// 校验源转换成数字
List<int> source = new List<int>();
for (int i = 0; i < checkSource.Length; i++)
{
source.Add(Convert.ToInt32(checkSource.Substring(i, 1)));
}
// 校验源的校验和
string checkLast = txt.Substring(17);
int sum = 0;
// 对应项求积,再把所有积求和
for (int i = 0; i < source.Count; i++)
{
sum += source[i] * check[i];
}
// 取余
int remainder = sum % 11;
if (string.Equals(checkLast, checkSum[remainder].ToString()))
{
res = true;
}
}
return res;
}