在网页上,通常是一个textbox让用户输入,然后用一个正则表达式验证控件,用这样一个正则表达式:\d{17}[\d|X]|\d{15}
这样当内容通过验证以后,可以保证用户输入的文本有15位数字或者18位数字(末尾可以为X)组成。
我们可以先声明一个string数组,当然int数组也可以,用string主要是因为可以胜率string转换到int的过程。
/* 身份证的省份地区代码(身份证的前两位)
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 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"
};
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 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"
};
如果身份证是18位的,它包含前6位地区码,中间8位出生日期,2位序号,最后1位校验数字。
最后一位校验数字是根据前17位计算出。
计算方法如下,(这一段是我转载的)
/*
* 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:
* 六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
* (比较原来的15位身份证,把出生年份扩展为4位,并增加了一位校验码。注意该位可能为X)
*
* 校验方法:
*
*(1)十七位数字本体码加权求和公式
* S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
*
* Ai:表示第i位置上的身份证号码数字值
* Wi:表示第i位置上的加权因子
* Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
*
* 2)计算模
* Y = mod(S, 11) //即Y=S%11;
*
* 3)通过模得到对应的校验码
* Y: 0 1 2 3 4 5 6 7 8 9 10
* 校验码: 1 0 X 9 8 7 6 5 4 3 2
*/
此外,我们还要检验一下,中间的出生日期是否是合法的日期形式即可。
校验的方法如下:
idstr参数为传入的用户输入字符串,如果校验成功,我们返回true并把出生日期填入第二个传入参数中。如果失败,返回false,并把验证失败的信息填充到第三个传入参数中。
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;
}
{
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;
}