
根据身份证号计算出出身日期
发布时间:2010-10-30 15:36:02 文章来源:jiaxingweb 浏览:[ ] 评论:[ ]
有个项目,客户要求通过身份证号计算出一个人的出生日期和年龄,按照通常的做法就是取出身份证上的几位数就可以了,从第7位开始取8位
数,就是出生日期可是身份证号现在有18位和15位之分,所以还要判断是多少位的身份证号,
但是只是想做一个简单的计算就可以了,可是在网上发现了“15位和18位身份证提取出生日期,性别和城市代码”,写的非常详细,通过身份
证号可以得到的数据,它都计算出来了,
本人也是借用了它的代码, 本段代码可以计算出出生日期、性别、城市。它所写的代码原理非常好,在我借用的过程中发现,身份证识别不了
,原来是它里面的正值表达式写的有错误,我修改了一下 ,还有就是,它在判断“非法证号”的时候,原来我是没有去考究,但是有好多身份证号得出错误的结果,如果你用不着的话,你就把这一段给删除,下面我将改好的代码贴出来,供大家参考!
本段代码是一个完整的类,这要建个类就可以了,而且都是用的静态方法,很方便调用!
using System;
using System.Collections.Generic
using System.Text;
using System.Text.RegularExpressions;
namespace C_Idcard
{
public class Idcard
{
public Idcard()
{
}
public static string validate(string cid)
{
string mResult;
//这句话是原来的,下面的是我改好的
//System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^d{17}(d|x)$");
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
System.Text.RegularExpressions.Match mc = rg.Match(cid);
//这句话是原来的,下面的是我改好的
//System.Text.RegularExpressions.Regex rg2 = new System.Text.RegularExpressions.Regex(@"^d{15}$");
System.Text.RegularExpressions.Regex rg2 = new System.Text.RegularExpressions.Regex(@"^/d{15}$");
System.Text.RegularExpressions.Match mc2 = rg.Match(cid);
if (mc.Success)
{
mResult = CheckCidInfo(cid);
return mResult;
}
else if (mc2.Success)
{
string str = per15To18(cid);
mResult = CheckCidInfo(str);
return mResult;
}
else
{ return "身份证位数不对"; }
}
public static string CheckCidInfo(string cid)
{
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="";
cid = cid.ToLower();
cid = cid.Replace("x","a");
if(aCity[int.Parse(cid.Substring(0,2))]==null)
{
return "非法地区";
}
try
{
DateTime.Parse(cid.Substring(6,4)+"-"+cid.Substring(10,2)+"-"+cid.Substring(12,2));
}
catch
{
return "非法生日";
}
// for(int i=17;i>=0;i--)
// {
// iSum +=(System.Math.Pow(2,i)%11)*int.Parse(cid[17-i].ToString
(),System.Globalization.NumberStyles.HexNumber);
// }
// if(iSum%11!=1)
// return("非法证号");
return(aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10,
2) + "-" + cid.Substring(12, 2) + "," + ((int.Parse(cid.Substring(16, 1)) % 2 == 1) ? "男" : "女"));
}
private static string per15To18(string perIDSrc)
{
int iS = 0;
//加权因子常数
int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
//校验码常数
string LastCode = "10X98765432";
//新身份证号
string perIDNew;
perIDNew = perIDSrc.Substring(0, 6);
//填在第6位及第7位上填上‘1’,‘9’两个数字
perIDNew += "19";
perIDNew += perIDSrc.Substring(6, 9);
//进行加权求和
for (int i = 0; i < 17; i++)
{
iS += int.Parse(perIDNew.Substring(i, 1)) * iW[i];
}
//取模运算,得到模值
int iY = iS % 11;
//从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
perIDNew += LastCode.Substring(iY, 1);
return perIDNew;
}
}
}
代码原文出自:http://blog.csdn.net/orichisonic/archive/2007/10/16/1827226.aspx
- 是否缺少 using 指令或程序集引用?
- 40个超实用的web设计工具(2)
- 40个超实用的web设计工具(1)
- JAVAScript 转义字符(Esc...
- Photoshop网页制作教程
- mssql2000存储过程调试
- command中CreateParam...
- 最常用网页技巧
- 推荐几本交互设计师书籍
- 国外开源基于C#的asp.net网站管...
- 减少网页大小,提高网页速度
- CSS命名规范及CSS书写规范
- 二十二个常用网页功能
- 你未必知道的10个CSS技巧
- 10个CSS简写技巧让你永远受用
- 中文域名工作原理
- 百度和Google搜索编码的区别
- ascii编码和gb2312编码和和U...
- 虚拟主机速度测试方法
- photoshop制作漂亮的3D文字
