找了一篇以往的文章, Sub CheckSID(ByVal sender As Object, ByVal e As ServerValidateEventArgs) 'Put user code to initialize the page here If Trim(Me._clientIDType) = "10" Then Dim arrVerifyCode() As String = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"} Dim Wi() As Byte = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} If Len(e.Value) < 15 Or Len(e.Value) = 16 Or Len(e.Value) = 17 Or Len(e.Value) > 18 Then e.GetType.ToString() sender.ErrorMessage = "身份证号共有15位或18位" e.IsValid = False Exit Sub End If Dim Ai As String If Len(e.Value) = 18 Then Ai = Mid(e.Value, 1, 17) ElseIf Len(e.Value) = 15 Then Ai = e.Value Ai = Left(Ai, 6) & "19" & Mid(Ai, 7, 9) End If If Not IsNumeric(Ai) Then sender.ErrorMessage = "身份证除最后一位外必须为数字" e.IsValid = False Exit Sub End If Dim strYear, strMonth, strDay If Len(Ai) = 15 Then strYear = CInt(Mid("19", 1, 2) & Mid(Ai, 7, 2)) strMonth = CInt(Mid(Ai, 9, 2)) strDay = CInt(Mid(Ai, 11, 2)) ElseIf Len(Ai) = 18 Then strYear = CInt(Mid(Ai, 7, 4)) strMonth = CInt(Mid(Ai, 11, 2)) strDay = CInt(Mid(Ai, 13, 2)) End If Dim BirthDay As DateTime = DateSerial(strYear, strMonth, strDay) If IsDate(BirthDay) Then If DateDiff(DateInterval.Year, Now, BirthDay) < -140 Or Format(BirthDay, "yyyyMMdd") > Format(Now, "yyyyMMdd") Then sender.ErrorMessage = "证件号校验有误,请按取消键重新输入" e.IsValid = False Exit Sub End If If strMonth > 12 Or strDay > 31 Then sender.ErrorMessage = "证件号校验有误,请按取消键重新输入" e.IsValid = False Exit Sub End If Else sender.ErrorMessage = "证件号校验有误,请按取消键重新输入" e.IsValid = False Exit Sub End If Dim i, TotalmulAiWi As Integer For i = 0 To 16 TotalmulAiWi = TotalmulAiWi + CInt(Mid(Ai, i + 1, 1)) * Wi(i) Next Dim modValue As Byte modValue = TotalmulAiWi Mod 11 Dim strVerifyCode As String strVerifyCode = arrVerifyCode(modValue) Ai = Ai & strVerifyCode If Len(e.Value) = 18 And e.Value <> Ai Then sender.ErrorMessage = "证件号校验有误,请按取消键重新输入" e.IsValid = False Exit Sub End If If Len(e.Value) = 18 Then If Me.dtcBirthDate.Value <> BirthDay Then sender.ErrorMessage = "身份证中日期与所选出生日期不一致" e.IsValid = False Exit Sub End If End If End If End Sub Public Function GetBirthDate(ByVal Ai As String) As DateTime Dim strYear, strMonth, strDay If Len(Ai) = 15 Then strYear = CInt(Mid("19", 1, 2) & Mid(Ai, 7, 2)) strMonth = CInt(Mid(Ai, 9, 2)) strDay = CInt(Mid(Ai, 11, 2)) ElseIf Len(Ai) = 18 Then strYear = CInt(Mid(Ai, 7, 4)) strMonth = CInt(Mid(Ai, 11, 2)) strDay = CInt(Mid(Ai, 13, 2)) End If Dim BirthDay As DateTime = DateSerial(strYear, strMonth, strDay) Return BirthDay End Function<Asp:CustomValidator Runat="Server" Id="CustomValidator1" ControlToValidate="txtClientIDNum" OnServerValidate="CheckSID" />
{
int[] W = new int[] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
char[] A= new char[] {'1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2'};
int j,S;
string NewID=ID18.Substring(0,17);
S=0;
for (int i=0;i<17;i++)
{
j=int.Parse(NewID[i].ToString())*W[i];
S=S+j;
}
S=S % 11;
return ID18[17]==A[S];
}
'Put user code to initialize the page here
If Trim(Me._clientIDType) = "10" Then Dim arrVerifyCode() As String = {"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"}
Dim Wi() As Byte = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} If Len(e.Value) < 15 Or Len(e.Value) = 16 Or Len(e.Value) = 17 Or Len(e.Value) > 18 Then
e.GetType.ToString()
sender.ErrorMessage = "身份证号共有15位或18位"
e.IsValid = False Exit Sub
End If Dim Ai As String
If Len(e.Value) = 18 Then
Ai = Mid(e.Value, 1, 17)
ElseIf Len(e.Value) = 15 Then
Ai = e.Value
Ai = Left(Ai, 6) & "19" & Mid(Ai, 7, 9)
End If
If Not IsNumeric(Ai) Then
sender.ErrorMessage = "身份证除最后一位外必须为数字"
e.IsValid = False Exit Sub
End If Dim strYear, strMonth, strDay If Len(Ai) = 15 Then
strYear = CInt(Mid("19", 1, 2) & Mid(Ai, 7, 2))
strMonth = CInt(Mid(Ai, 9, 2))
strDay = CInt(Mid(Ai, 11, 2))
ElseIf Len(Ai) = 18 Then
strYear = CInt(Mid(Ai, 7, 4))
strMonth = CInt(Mid(Ai, 11, 2))
strDay = CInt(Mid(Ai, 13, 2))
End If Dim BirthDay As DateTime = DateSerial(strYear, strMonth, strDay) If IsDate(BirthDay) Then
If DateDiff(DateInterval.Year, Now, BirthDay) < -140 Or Format(BirthDay, "yyyyMMdd") > Format(Now, "yyyyMMdd") Then
sender.ErrorMessage = "证件号校验有误,请按取消键重新输入"
e.IsValid = False
Exit Sub
End If
If strMonth > 12 Or strDay > 31 Then
sender.ErrorMessage = "证件号校验有误,请按取消键重新输入"
e.IsValid = False
Exit Sub
End If
Else
sender.ErrorMessage = "证件号校验有误,请按取消键重新输入"
e.IsValid = False
Exit Sub
End If Dim i, TotalmulAiWi As Integer
For i = 0 To 16
TotalmulAiWi = TotalmulAiWi + CInt(Mid(Ai, i + 1, 1)) * Wi(i)
Next
Dim modValue As Byte
modValue = TotalmulAiWi Mod 11
Dim strVerifyCode As String
strVerifyCode = arrVerifyCode(modValue)
Ai = Ai & strVerifyCode If Len(e.Value) = 18 And e.Value <> Ai Then
sender.ErrorMessage = "证件号校验有误,请按取消键重新输入"
e.IsValid = False
Exit Sub
End If If Len(e.Value) = 18 Then
If Me.dtcBirthDate.Value <> BirthDay Then
sender.ErrorMessage = "身份证中日期与所选出生日期不一致"
e.IsValid = False
Exit Sub
End If
End If
End If
End Sub Public Function GetBirthDate(ByVal Ai As String) As DateTime
Dim strYear, strMonth, strDay If Len(Ai) = 15 Then
strYear = CInt(Mid("19", 1, 2) & Mid(Ai, 7, 2))
strMonth = CInt(Mid(Ai, 9, 2))
strDay = CInt(Mid(Ai, 11, 2))
ElseIf Len(Ai) = 18 Then
strYear = CInt(Mid(Ai, 7, 4))
strMonth = CInt(Mid(Ai, 11, 2))
strDay = CInt(Mid(Ai, 13, 2))
End If Dim BirthDay As DateTime = DateSerial(strYear, strMonth, strDay)
Return BirthDay
End Function<Asp:CustomValidator Runat="Server" Id="CustomValidator1" ControlToValidate="txtClientIDNum" OnServerValidate="CheckSID" />