public static bool ValidIndentity(string identityNum)
{
if (!string.IsNullOrEmpty(identityNum) && identityNum.Length == 18)
{
//判断身份证是否合法
for (int i = 0; i < 18; i++)
{
if (i < 17)
{
//char类型的1对应int是49 好坑
int num = 0;
bool isNum = int.TryParse(identityNum[i].ToString(), out num);
if (isNum)
{
if (num > 9 || num < 0)
{
return false;
}
}
else
{
return false;
}
}
if (i == 17)
{
char[] c = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'x' };
if (!c.Contains(identityNum[i]))
{
return false;
}
}
}
string birth = identityNum.Substring(6, 8);
DateTime birthDt;
DateTime.TryParseExact(birth, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out birthDt);
TimeSpan ts = DateTime.Now - birthDt;
if (ts.TotalDays / 365 > 18)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}