/// <summary>
/// 检验身份证号码,如果无效的身份证号返回空字符,否则返回生日+性别,如1978-08-180,0为女,1为男
/// </summary>
/// <param name="sID">身份证号码</param>
/// <returns></returns>
public static string CheckID(string sID)
{
string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"};
double iSum=0;
string info="";
string sBirth="",sSex="";
if(sID.Length != 15 && sID.Length != 18)
{
MessageBox.Show("无效的身份证号,身份证号的长度是15位或18位。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
else if(sID.Length == 15)
{
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{15}$");
System.Text.RegularExpressions.Match mc = rg.Match(sID);
if(!mc.Success)
{
MessageBox.Show("无效的身份证号。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}   
sID = sID.ToLower();
sID = sID.Replace("x","a");
if(int.Parse(sID.Substring(0,2))>aCity.Length || aCity[int.Parse(sID.Substring(0,2))]==null)
{
MessageBox.Show("无效的身份证号,地区信息错误。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
sBirth=SysDate.FormatStringToDate(sID.Substring(6,6),0,"-");
if(sBirth=="")
{
MessageBox.Show("无效的身份证号,生日信息错误。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
int i=int.Parse(sID.Substring(14,1))%2;
sSex=i.ToString();
}
else
{
// string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"};
// double iSum=0;
// string info="";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(sID);
if(!mc.Success)
{
return "";
}   
sID = sID.ToLower();
sID = sID.Replace("x","a");
if(int.Parse(sID.Substring(0,2))>aCity.Length || aCity[int.Parse(sID.Substring(0,2))]==null)
{
MessageBox.Show("无效的身份证号,地区信息错误。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
try
{
DateTime.Parse(sID.Substring(6,4)+"-"+sID.Substring(10,2)+"-"+sID.Substring(12,2));
}
catch
{
MessageBox.Show("无效的身份证号,生日信息错误。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
for(int i=17;i>=0;i--)
{    
iSum +=(System.Math.Pow(2,i)%11)*int.Parse(sID[17-i].ToString(),System.Globalization.NumberStyles.HexNumber); }
if(iSum%11!=1)
{
MessageBox.Show("无效的身份证号。","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information);
return "";
}
sBirth=SysDate.FormatStringToDate(sID.Substring(6,8),0,"-");
int j=int.Parse(sID.Substring(16,1))%2;
sSex=j.ToString(); //return(aCity[int.Parse(sID.Substring(0,2))]+","+sID.Substring(6,4)+"-"+sID.Substring(10,2)+"-"+sID.Substring(12,2)+","+(int.Parse(sID.Substring(16,1))%2==1,"男":"女"));
}
return sBirth+sSex;
}

解决方案 »

  1.   

    身份证号码里面的位数和数字所在位置是固定的,用Substring就可以获得。
    "101126(197701224)1716".Substring(6,8);就可以了。
      

  2.   

    用substring(6,8)就一个身份证号来说接下来怎样写 能说具体点吗?谢谢
      

  3.   

    我现在SQL数据库中有一个身份证号的数据表 我想在WebFrom中拉一个Button控件,点击一下就能导出所有的身份证号中的出生年月日到另一个数据表中 这个该怎么去编写 谢谢
      

  4.   

    先撈出所有的紀錄(用substring()處理)存放在DataSet中,然後做Insert到另一個表裡面就好了.