private string[] m_AreaCodes =
{
"11","12","13","14","15",
"21","22","23",
"31","32","33","34","35","36","37",
"41","42","43","44","45","46",
"50","51","52","53","54",
"61","62","63","64","65",
"71",
"81","82",
"91"
};
private bool isValidIdCardNo(string idstr, out DateTime birthdate, out string errorMsg)
{
errorMsg = null;
birthdate = DateTime.Now;
//校验前两位是否是国内某个地区的代码
if (Array.IndexOf(this.m_AreaCodes, idstr.Substring(0, 2)) < 0)
{
errorMsg = "您输入的身份证号码不正确(没有通过所属地区代码的验证)!";
return false;
} bool isDate = false;
if (idstr.Length == 18)
{
string checkcode = "10X98765432";
//加权因子
int[] factors = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
//计算加权和
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += (idstr[i] - '0') * factors[i]; //48是‘0’的ascii码
}
char c = checkcode[(sum % 11)];
if (idstr[17] != c)
{
errorMsg = "身份证校验码错误!";
return false;
}
//校验一下用户输入的日期段是否是真正的日期
isDate = DateTime.TryParse(string.Format("{0}{1}{2}{3}-{4}{5}-{6}{7}",
idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11], idstr[12], idstr[13]), out birthdate);
}
else if (idstr.Length == 15)
{
//校验一下用户输入的日期段是否是真正的日期
isDate = DateTime.TryParse(string.Format("19{0}{1}-{2}{3}-{4}{5}",
idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11]), out birthdate); }
else
{
errorMsg = "身份证位数不正确(应为15位或者18位)!";
return false;
}
if (!isDate)
{
errorMsg = "身份证号码的出生日期部分不正确!";
return false;
}
return true;
}这是一个写好的方法,目的是判断身份证的真假.窗体有一个textbox,一个button,就是不会用.不会调用呀.现在想来自已是一笨驴...
各位大侠...教教这头驴吧.
{
"11","12","13","14","15",
"21","22","23",
"31","32","33","34","35","36","37",
"41","42","43","44","45","46",
"50","51","52","53","54",
"61","62","63","64","65",
"71",
"81","82",
"91"
};
private bool isValidIdCardNo(string idstr, out DateTime birthdate, out string errorMsg)
{
errorMsg = null;
birthdate = DateTime.Now;
//校验前两位是否是国内某个地区的代码
if (Array.IndexOf(this.m_AreaCodes, idstr.Substring(0, 2)) < 0)
{
errorMsg = "您输入的身份证号码不正确(没有通过所属地区代码的验证)!";
return false;
} bool isDate = false;
if (idstr.Length == 18)
{
string checkcode = "10X98765432";
//加权因子
int[] factors = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
//计算加权和
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += (idstr[i] - '0') * factors[i]; //48是‘0’的ascii码
}
char c = checkcode[(sum % 11)];
if (idstr[17] != c)
{
errorMsg = "身份证校验码错误!";
return false;
}
//校验一下用户输入的日期段是否是真正的日期
isDate = DateTime.TryParse(string.Format("{0}{1}{2}{3}-{4}{5}-{6}{7}",
idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11], idstr[12], idstr[13]), out birthdate);
}
else if (idstr.Length == 15)
{
//校验一下用户输入的日期段是否是真正的日期
isDate = DateTime.TryParse(string.Format("19{0}{1}-{2}{3}-{4}{5}",
idstr[6], idstr[7], idstr[8], idstr[9], idstr[10], idstr[11]), out birthdate); }
else
{
errorMsg = "身份证位数不正确(应为15位或者18位)!";
return false;
}
if (!isDate)
{
errorMsg = "身份证号码的出生日期部分不正确!";
return false;
}
return true;
}这是一个写好的方法,目的是判断身份证的真假.窗体有一个textbox,一个button,就是不会用.不会调用呀.现在想来自已是一笨驴...
各位大侠...教教这头驴吧.
解决方案 »
- 微软的数据同步框架 Microsoft Sychronization Framework 2.0
- 本机框架位于调用堆栈的顶部,因此无法计算表达式的值
- 请问datagridview里面的datagridviewcheckboxcolumn列里面的checkbox后面可以跟文字吗?
- 崩溃,vs2005安装总提示'找不到baseline数据'
- 关于多线程 大家有没有易懂的资料或者案例
- webbrower中如何自动点网页上的超链接???
- 请教一个XML文件的读取,在线等答案
- c#接口问题汇总
- 高分!请高手帮我转这段代码从C++到C#,能转几个方法几个方法,这段代码调用了另一个C++的DLL:ISO8211.dll!请高手尽快
- AppSettings问题请叫.急急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!在线等待.!!1
- 关于 创建 文件夹快捷方式
- C# 3.0 的匿名类是否可以定义属性或者函数?
string errorMsg;
string idstr="12345667889899";
if(isValidIdCardNo(idstr,out birthdate,outerrorMsg)
{
.....
}
else
{
.....
}
string identifierStr=textBox.Text;
//随便给定一个日期,这里的xxx,yy,zz为整数
DateTime dt;
string error;isValidIdCardNo(identifierStr,dt,out error);
、、
这样就可以调用此函数了!
isValidIdCardNo("",out dt,out str)
正则判断身份证
public static string CheckIDCard(string pId)
{
string[] arrVerifyCode = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
int[] Checker = { 1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1 };
if (pId.Length != 15 && pId.Length != 18) throw new FormatException("身份证号码只有15位或18位");
string Ai = pId.Length == 18 ? pId.Substring(0, 17) : pId.Substring(0, 6) + "19" + pId.Substring(6, 16);
if (!Regex.IsMatch(Ai, @"^\d{17}")) throw new FormatException("身份证除最后一位外,必须为数字!");
int year = int.Parse(Ai.Substring(6, 4));
DateTime birthday = new DateTime(year, int.Parse(Ai.Substring(10, 2)), int.Parse(Ai.Substring(12, 2)));
int ret = 0;
for (int i = 0; i < 17; i++) ret += int.Parse(Ai.Substring(i, 1)) * Wi[i];
Ai += arrVerifyCode[ret %= 11];
return pId.Length == 18 && pId.ToLower() != Ai ? "身份证输入错误!" : Ai;
}
wuyq11更复杂了,更不会用了.
to:
kensouterry可否再说详细点;
to:
soaringbird
可否再说详细点;
/// button的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DateTime birthdate;
string errorMsg;
string idstr="511533242352355"; //这里是需要证实的身份证号号码,你自己定义
if(isValidIdCardNo(idstr,out birthdate,outerrorMsg))//在这个if语句里,调用方法
{
MessageBox .Show ("身份证是真的");
}
else
{
MessageBox .Show ("身份证是假的");
}
}
这样你应该明白了 ,你只需要单击你from里的button,里面就可以是上面的代码
string errorMsg;
string idstr="12345667889899";
if(isValidIdCardNo(idstr,out birthdate,out errorMsg)
{
//是真的
Console.WriteLine("此人的生日是:{0}",birthdate);
}
else
{
//是假的
Console.WriteLine("此为假的证,错误信息:{0}",errorMsg);
}
/// button的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DateTime birthdate;
string errorMsg;
string idstr="511533242352355"; //这里是需要证实的身份证号号码,你自己定义
if(isValidIdCardNo(idstr,out birthdate,out errorMsg))//在这个if语句里,调用方法
{
MessageBox .Show ("身份证是真的");
}
else
{
MessageBox .Show ("身份证是假的");
}
}
修改一下