/// <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;
}
/// 检验身份证号码,如果无效的身份证号返回空字符,否则返回生日+性别,如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;
}
解决方案 »
- 一段代码 别的代码枚问题 我是复制别的页面的 就段代码我改了 不显示结果了
- visual studio2010+SQL2008生成安装包的问题
- 项目的main方法的[STAThread]属性设置线程为STA无效, 依然是MTA!老是报OLE之类的错误。有谁遇到过
- 100分求在GridView中绑定DropDownList,在线等
- keybd_event按键按下后不能抬起?
- 如何把SQL2005里的数据导出到EXECL中?具体在代码上怎么写?我是个初学者。
- 该死的常出现的异常,如下
- windows ce可以用MySql做数据库么~?
- 请教一个比较菜的问题,如何使ShowDialog()出来的窗体位置在当前窗口的正中央?
- C#制作可伸缩窗体,进行重绘时的坐标问题,帖出源码,望高手帮忙解决!
- 畫曲線圖或柱圖時,怎樣得到要標題字的長度[用來居中用的]!!刻度為1CM應該怎麼畫
- 如何将C#开发的程序在没有安装.net framework的机器上使用?
"101126(197701224)1716".Substring(6,8);就可以了。