asp.net中验证身份证号
asp.net中,如何用js或类函数来验证输入的身份证号呢?拜托各位高手指点~ --------------------编程问答-------------------- 3 Public Class SFZ
4 Private _code As String
5
6 Public Sub New(ByVal code As String)
7 _code = code
8 End Sub
9
10 Public Function Verify() As Boolean
11 '返回结果
12 Dim result As Boolean = False
13 Dim datestr As System.Text.StringBuilder = New System.Text.StringBuilder
14
15 Select Case _code.Length
16 Case 15
17 '取得日期字符串
18 datestr.Append("19")
19 datestr.Append(_code.Substring(6, 2))
20 datestr.Append("-")
21 datestr.Append(_code.Substring(8, 2))
22 datestr.Append("-")
23 datestr.Append(_code.Substring(10, 2))
24 '判断是否为日期
25 result = IsDate(datestr.ToString())
26 Case 18
27 '取得日期字符串
28 datestr.Append(_code.Substring(6, 4))
29 datestr.Append("-")
30 datestr.Append(_code.Substring(10, 2))
31 datestr.Append("-")
32 datestr.Append(_code.Substring(12, 2))
33 '判断是否为日期
34 result = IsDate(datestr.ToString())
35 If result Then
36 '判断校验码是否正确
37 If _code.Substring(17, 1) = lastCodeVerify(_code.Substring(0, 17)) Then
38 result = True
39 Else
40 result = False
41 End If
42 End If
43 Case Else
44 result = False
45 End Select
46
47 Verify = result
48 End Function
49
50 Private Function lastCodeVerify(ByVal a_sfz As String) As String
51 '获得校验码
52 Dim lastcode As String
53 Dim ls_sfz As String
54 Dim i, li_ai, li_wi, li_sum, li_mod, li_result As Integer
55
56 ls_sfz = a_sfz
57 If ls_sfz.Length = 15 Or ls_sfz.Length = 17 Then
58 If ls_sfz.Length = 15 Then
59 '应该不会执行
60 ls_sfz = ls_sfz.Substring(0, 6) + "19" + ls_sfz.Substring(6, 9)
61 End If
62
63 For i = 2 To 18
64 li_ai = CInt(ls_sfz.Substring(19 - i - 1, 1))
65 li_wi = System.Math.Pow(2, i - 1) Mod 11
66 li_sum = li_sum + li_ai * li_wi
67 Next
68
69 li_mod = li_sum Mod 11
70 li_result = 12 - li_mod
71
72 If li_result >= 10 Then
73 If li_result = 10 Then
74 lastcode = "X"
75 Else
76 li_result = li_result - 11
77 lastcode = CStr(li_result)
78 End If
79 Else
80 lastcode = CStr(li_result)
81 End If
82 Else
83 lastcode = "E"
84 End If
85
86 lastCodeVerify = lastcode
87
88 End Function
89 End Class
--------------------编程问答--------------------
/*--------------------编程问答-------------------- 想验证这个,你必须身份证号的生成规则才行,可是我也不知道啊 --------------------编程问答-------------------- private string CheckCidInfo(string cid)
身份证判断函数,是返回true,不是返回false
15位数字,18位数字或者最后一位为X(大写)
*/
function isSFZ(str){
if(""==str){
return false;
}
if(str.length!=15&&str.length!=18){//身份证长度不正确
return false;
}
if(str.length==15){
if(!isNumber(str)){
return false;
}
}else{
str1 = str.substring(0,17);
str2 = str.substring(17,18);
alpha = "X0123456789";
if(!isNumber(str1)||alpha.indexOf(str2)==-1){
return false;
}
}
return true;
}
/*
{
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(cid);
if(!mc.Success)
{
return "";
}
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¡"男":"女"));
} --------------------编程问答-------------------- 这个好像不知道,
想像中不可以实现的办法就是连上易做图部的电脑,进行数据比较 --------------------编程问答-------------------- function checkIdcard(idcard){
var Errors=new Array(
"验证通过!",
"身份证号码位数不对!",
"身份证号码出生日期超出范围或含有非法字符!",
"身份证号码校验错误!",
"身份证地区非法!"
);
var area={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:"国外"}
var idcard,Y,JYM;
var S,M;
var idcard_array = new Array();
idcard_array = idcard.split("");
//地区检验
if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4];
alert(Errors[4]);
//身份号码位数及格式检验
switch(idcard.length){
case 15:
if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
} else {
ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
}
if(ereg.test(idcard)) return Errors[0];
alert(Errors[0]);
else return Errors[2];
alert(Errors[2]);
break;
case 18:
//18位身份号码检测
//出生日期的合法性检查
//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
} else {
ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
}
if(ereg.test(idcard)){//测试出生日期的合法性
//计算校验位
S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
+ parseInt(idcard_array[7]) * 1
+ parseInt(idcard_array[8]) * 6
+ parseInt(idcard_array[9]) * 3 ;
Y = S % 11;
M = "F";
JYM = "10X98765432";
M = JYM.substr(Y,1);//判断校验位
if(M == idcard_array[17]) return Errors[0]; //检测ID的校验位
else return Errors[3];
alert(Errors[3]);
}
else return Errors[2];
alert(Errors[2]);
break;
default:
return Errors[1];
alert(Errors[1]);
break;
}
}
--------------------编程问答-------------------- 第一个发的是VB的到中国统计网站查询代码 --------------------编程问答-------------------- 这些可以用于asp.net么? --------------------编程问答--------------------
当然可以了。正则在哪儿都可以用到
--------------------编程问答-------------------- 2楼的if(""==str)..应该是(str=="")吧.. --------------------编程问答--------------------
这样也可以 --------------------编程问答-------------------- 感觉主要是两个验证:
1.是15位或18位数字的正则表达式的验证
2.验证身份证中年月日是正常的年月日的日期验证。 --------------------编程问答-------------------- 用正则表达式
1.验证身份证号(18位):^\d{}18$
2.验证身份证号(15位):^\d{15}
3.验证身份证号(15位或18位):^\d{15}|\d{}18$
--------------------编程问答-------------------- 我一直都搞不清楚"^\d{15} ¦\d{}18$ "这个到底要怎么用...泪奔... --------------------编程问答-------------------- 我在6楼给你提供了正则表达式和用法,自己看看就明白了 --------------------编程问答-------------------- //身份证正则表达式(15位)
isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
//身份证正则表达式(18位)
isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
再PS个
这个是别人的,我们没有用,
更健壮的 地区和身份证的关系也判断到 还有性别
<script>
var
aCity={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:"国外"}
function cidInfo(sId){
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(sId))return false;
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:非法地区";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:非法生日";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error:非法证号";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
}
</script>
补充:.NET技术 , ASP.NET