none
vb2008 身份證檢查碼問題 非ASP.net RRS feed

  • 問題

  • 親愛的各位大大:

          以下是我從網路上找到的一段 身份證檢查碼 改來改去還是有問題所以只好上來麻煩各位大大幫我解。

    Function CheckIdcode(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
    Dim Letter As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim MirrorNumber() = {10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 30, 41, 42, 33}
    Dim Checker() = {1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1}
    Dim N1 As String = UCase(Left(e.Value, 1))
    Dim I, N1Ten, N1Unit, Total As Integer
    Dim Pos As Byte = InStr(Letter, N1)
    If Pos <= 0 Then
    sender.ErrorMessage = "申請人身份證字號第 1 碼必須為英文字母"
    e.IsValid = False
    Exit Sub
    End If
    If Len(e.Value) < 10 Then
    sender.ErrorMessage = "申請人身份證字號共有 10 碼"
    e.IsValid = False
    Exit Sub
    End If
    For I = 2 To 10
    If Not (Asc(Mid(e.Value, I, 1)) >= 48 And Asc(Mid(e.Value, I, 1)) <= 57) Then
    sender.ErrorMessage = "申請人身份證字號第 2 ~ 9 碼必須為數字"
    e.IsValid = False
    Exit Sub
    End If
    Next
    If Not (Mid(e.Value, 2, 1) = "1" Or Mid(e.Value, 2, 1) = "2") Then
    sender.ErrorMessage = "申請人身份證字號第 2 碼必須為 1 或 2"
    e.IsValid = False
    Exit Sub
    End If
    N1Ten = Mid(MirrorNumber(Pos - 1), 1, 1)
    N1Unit = Mid(MirrorNumber(Pos - 1), 2, 1)
    Total = N1Ten * Checker(0) + N1Unit * Checker(1)
    For I = 2 To 10
    Total = Total + Mid(e.Value, I, 1) * Checker(I)
    Next
    If Total Mod 10 = 0 Then
    e.IsValid = True
    Else
    sender.ErrorMessage = "申請人身份證字號輸入錯誤!"
    e.IsValid = False
    Exit Sub
    End If
    End function

    問題1:它說ByVal e As ServerValidateEventArgs 這個有問題
    問題2:它說 Public property left() as Integer沒有引數無法對其索引
    問題3:它說e.Value 不是 system.window.forms.type.validationEvenarges的成員

    請問各位大大以上要怎麼改才能使,使用者在輸入身份證字號到textbox時就進行檢核
    感謝各位大大

     

    2010年3月1日 上午 03:31

解答

  • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    	Dim value As String = TextBox1.Text.Trim
    	Dim Letter As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	Dim MirrorNumber() = {10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 30, 41, 42, 33}
    	Dim Checker() = {1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1}
    	Dim N1 As String = UCase(Microsoft.VisualBasic.Left(value, 1))
    	Dim I, N1Ten, N1Unit, Total As Integer
    	Dim Pos As Byte = InStr(Letter, N1)
    	If Pos <= 0 Then
    		MessageBox.Show("申請人身份證字號第 1 碼必須為英文字母")
    		Exit Sub
    	End If
    	If Len(value) < 10 Then
    		MessageBox.Show("申請人身份證字號共有 10 碼")
    		Exit Sub
    	End If
    	For I = 2 To 10
    		If Not (Asc(Mid(value, I, 1)) >= 48 And Asc(Mid(value, I, 1)) <= 57) Then
    			MessageBox.Show("申請人身份證字號第 2 ~ 9 碼必須為數字")
    			Exit Sub
    		End If
    	Next
    	If Not (Mid(value, 2, 1) = "1" Or Mid(value, 2, 1) = "2") Then
    		MessageBox.Show("申請人身份證字號第 2 碼必須為 1 或 2")
    		Exit Sub
    	End If
    	N1Ten = Mid(MirrorNumber(Pos - 1), 1, 1)
    	N1Unit = Mid(MirrorNumber(Pos - 1), 2, 1)
    	Total = N1Ten * Checker(0) + N1Unit * Checker(1)
    	For I = 2 To 10
    		Total = Total + Mid(value, I, 1) * Checker(I)
    	Next
    	If Total Mod 10 = 0 Then
    		MessageBox.Show("正確")
    	Else
    		MessageBox.Show("申請人身份證字號輸入錯誤!")
    		Exit Sub
    	End If
    	'End Sub
    End Sub
    你找來的應該是ASP.NET驗證的範例吧..........
    要稍微改一下才能用
    2010年3月1日 上午 03:42

所有回覆

  • Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    	Dim value As String = TextBox1.Text.Trim
    	Dim Letter As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	Dim MirrorNumber() = {10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 30, 41, 42, 33}
    	Dim Checker() = {1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1}
    	Dim N1 As String = UCase(Microsoft.VisualBasic.Left(value, 1))
    	Dim I, N1Ten, N1Unit, Total As Integer
    	Dim Pos As Byte = InStr(Letter, N1)
    	If Pos <= 0 Then
    		MessageBox.Show("申請人身份證字號第 1 碼必須為英文字母")
    		Exit Sub
    	End If
    	If Len(value) < 10 Then
    		MessageBox.Show("申請人身份證字號共有 10 碼")
    		Exit Sub
    	End If
    	For I = 2 To 10
    		If Not (Asc(Mid(value, I, 1)) >= 48 And Asc(Mid(value, I, 1)) <= 57) Then
    			MessageBox.Show("申請人身份證字號第 2 ~ 9 碼必須為數字")
    			Exit Sub
    		End If
    	Next
    	If Not (Mid(value, 2, 1) = "1" Or Mid(value, 2, 1) = "2") Then
    		MessageBox.Show("申請人身份證字號第 2 碼必須為 1 或 2")
    		Exit Sub
    	End If
    	N1Ten = Mid(MirrorNumber(Pos - 1), 1, 1)
    	N1Unit = Mid(MirrorNumber(Pos - 1), 2, 1)
    	Total = N1Ten * Checker(0) + N1Unit * Checker(1)
    	For I = 2 To 10
    		Total = Total + Mid(value, I, 1) * Checker(I)
    	Next
    	If Total Mod 10 = 0 Then
    		MessageBox.Show("正確")
    	Else
    		MessageBox.Show("申請人身份證字號輸入錯誤!")
    		Exit Sub
    	End If
    	'End Sub
    End Sub
    你找來的應該是ASP.NET驗證的範例吧..........
    要稍微改一下才能用
    2010年3月1日 上午 03:42
  • 感謝大大你的解說

    2010年3月1日 上午 06:03