using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IDCard_Query
{
public class IDCard
{
public static string Tips = "";
public static string CheckSum(string IDCardNumber)
{
//
if (IDCardNumber.Length != 15 && IDCardNumber.Length != 17 && IDCardNumber.Length != 18)
{
Tips = " 输入的身份证号码应该是18位或15位,请重新输入!";
return IDCardNumber + "";
}
if (IDCardNumber.Length == 15 || IDCardNumber.Length == 17 && IDCardNumber.Length == 18)
{
Tips = "根据国际编码规则解读非认证结果,仅供参考";
}
if (IDCardNumber.Length < 17)
{
return IDCardNumber + "";
}
string xs = "79:584216379:5842";
int total = 0;
for (int i = 0; i < xs.Length; i++)
{
total += (IDCardNumber[i] - 48) * (xs[i] - 48);
}
int result = total % 11;
string results = "";
switch (result)
{
case 0: results = "1"; break;
case 1: results = "0"; break;
case 2: results = "x"; break;
case 3: results = "9"; break;
case 4: results = "8"; break;
case 5: results = "7"; break;
case 6: results = "6"; break;
case 7: results = "5"; break;
case 8: results = "4"; break;
case 9: results = "3"; break;
case 10: results = "2"; break;
}
if (IDCardNumber.Length == 18)
{
if (IDCardNumber.ToLower()[17] == results[0])
{
Tips = "根据国际编码规则解读非认证结果,仅供参考";
}
else
{
Tips = "输入的身份证号码校验位错误,“参考号码”的信息供您参考";
}
}
if (IDCardNumber.Length == 17)
{
return IDCardNumber + results;
}
else
{
IDCardNumber = IDCardNumber.Remove(17);
return IDCardNumber + results;
}
}
public static string Sex(string IDCardNumber)
{
if (IDCardNumber.Length < 17) return "";
int a = (IDCardNumber[16] - 48) % 2;
return (a == 0) ? "女" : "男";
}
public static string Birthday(string IDCardNumber)
{
if (IDCardNumber.Length < 14) return "";
if (IDCardNumber.Length == 15)
{
return string.Format("生日:19{0}年{1}月{2}日", IDCardNumber.Substring(6, 2), IDCardNumber.Substring(8, 2), IDCardNumber.Substring(10, 2));
}
else
{
return string.Format("生日:{0}年{1}月{2}日", IDCardNumber.Substring(6, 4), IDCardNumber.Substring(10, 2), IDCardNumber.Substring(12, 2));
}
}
public static string BirthdayYear(string IDCardNumber)
{
if (IDCardNumber.Length < 15) return "";
Check(IDCardNumber);
if (IDCardNumber.Length == 15)
return "19" + IDCardNumber.Substring(6, 2);
else
return IDCardNumber.Substring(6, 4);
// 输入的身份证号码有误,请重新输入!
}
public static string BirthdayMouth(string IDCardNumber)
{
if (IDCardNumber.Length < 15) return "";
if (IDCardNumber.Length == 15)
return IDCardNumber.Substring(8, 2);
else
return IDCardNumber.Substring(10, 2);
// 输入的身份证号码有误,请重新输入!
}
public static void Check(string IDCardNumber)
{
if (IDCardNumber.Length < 15) return;
string year = "";
if (IDCardNumber.Length == 15)
year = "19" + IDCardNumber.Substring(6, 2);
else
year = IDCardNumber.Substring(6, 4);
string mouth = "";
if (IDCardNumber.Length == 15)
mouth = IDCardNumber.Substring(8, 2);
else
mouth = IDCardNumber.Substring(10, 2);
string day = "";
if (IDCardNumber.Length == 15)
day = IDCardNumber.Substring(10, 2);
else
day = IDCardNumber.Substring(12, 2);
try
{
int y = int.Parse(year);
int m = int.Parse(mouth);
int d = int.Parse(day);
DateTime dt = new DateTime(y, m, d);
if (dt.Year == y && dt.Month == m && dt.Day == d)
{
}
else
{
Tips = "输入的身份证号码有误,请重新输入!";
}
}
catch (Exception exp)
{
Tips = "输入的身份证号码有误,请重新输入!";
}
}
public static string BirthdayDay(string IDCardNumber)
{
if (IDCardNumber.Length < 15) return "";
string day = "";
if (IDCardNumber.Length == 15)
day = IDCardNumber.Substring(10, 2);
else
day = IDCardNumber.Substring(12, 2);
DateTime dt = new DateTime();
// 输入的身份证号码有误,请重新输入!
return day;
}
public static string Address(string IDCardNumber)
{
if (IDCardNumber.Length < 6) return IDCardNumber;
string[] allAdress = AddressDatabase.IDCARDPRE6.Split(new string[] { "\r\n" }, StringSplitOptions.None);
string idcardAdressNumber = IDCardNumber.Substring(0, 6);
foreach (var item in allAdress)
{
if (item == null) continue;
string[] infos = item.Split(new string[] { "\t" }, StringSplitOptions.None);
if (infos == null || infos.Length < 3) continue;
if (idcardAdressNumber == infos[1]) return infos[2];
}
return IDCardNumber.Substring(0, 6);
}
}
}