2015-11-13 75 views
0

我已經編寫了保存數據到數據庫的代碼,這段代碼工作正常。但是,在驗證代碼時,儘管驗證代碼在控制檯模式下工作,但我仍遇到一些問題。問題是,當我調用函數(見下面的代碼)CheckValidPassword()等等,他們似乎不會返回正確的值,並且當涉及到savebutton click事件中的If語句時,代碼類型會跳過它並只是通過datagridview將數據保存到數據庫。調用驗證函數的VB.Net問題

這是代碼。

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click 
    Dim ValidUserName, ValidPassword, ValidTeacherUsername As Boolean 
    Dim Username, Password, TeacherUsername As String 
    Username = txtStudentID.Text 
    Password = txtStudentPassword.Text 
    TeacherUsername = txtTeacherID.Text 
    ValidUsernameCheck(ValidUserName, Username) 
    ValidPasswordCheck(ValidPassword, Password) 
    ValidTeacherUsernameCheck(ValidTeacherUsername, TeacherUsername) 
    If ValidUsernameCheck(ValidUserName, Username) <> True Or ValidPasswordCheck(ValidPassword, Password) <> True Or ValidTeacherUsernameCheck(ValidTeacherUsername, TeacherUsername) <> True Then 
     MsgBox("Saving failed", MsgBoxStyle.OkOnly) 
     'Exit Sub 
    Else 
     Try 
      Dim dataAdapter As New OleDbDataAdapter 
      Dim DataTable As New DataTable 
      Dim DataSet As New DataSet 
      Connection.Open() ' the following decleration are used to save content to the table.  
      DataSet.Tables.Add(DataTable) 
      Dim SQLQuery As String = (<sql>SELECT * FROM Students</sql>) 
      dataAdapter = New OleDbDataAdapter(SQLQuery, Connection) 
      dataAdapter.Fill(DataTable) 
      Dim newRow As DataRow = DataTable.NewRow 
      With newRow ' the with statement allows you do repeatedly apply a property to a certain object 
       .Item("StudentID") = txtStudentID.Text ' these statements add the content of the text boxes to these respective fields in the database 
       .Item("TeacherID") = txtTeacherID.Text 
       .Item("StudentFirstName") = txtStudentFirstname.Text 
       .Item("StudentSurname") = txtStudentSurname.Text 
       .Item("StudentPassword") = txtStudentPassword.Text 
       .Item("StudentGroup") = cbxStudentGroup.Text 
      End With 
      DataTable.Rows.Add(newRow) 
      Dim Command As New OleDbCommandBuilder(dataAdapter) 
      dataAdapter.Update(DataTable) 'updates the table 
      Connection.Close() 
      ShowItems() ' displays the table 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
      Connection.Close() 
     End Try 
    End If 
End Sub 

以下是用於驗證三個關鍵數據位的三個函數。

Function ValidUsernameCheck(ByRef ValidUserName As Boolean, ByVal Username As String) As Boolean 
    Dim Valid1, Valid2 As Boolean 
    If Char.IsLetter(Mid(Username, 1, 3)) Then ' takes the first 3 characters of a user name to see if they are 
     ' letters 
     Valid1 = True 
    Else 
     Valid1 = False 
    End If 

    If Char.IsNumber(Mid(Username, 4, 8)) Then 'does the same with numbers, starting at char(4) and taking 8. 
     Valid2 = True 
    Else 
     Valid2 = False 
    End If 
    If Valid1 = True And Valid2 = True Then 
     ValidUsernameCheck = True 
    Else 
     ValidUsernameCheck = False 
    End If 
    Return ValidUsernameCheck 
End Function 

Function ValidTeacherUsernameCheck(ByRef ValidTeacherUsername As Boolean, ByVal TeacherUsername As String) As Boolean 
    Dim Valid1, Valid2 As Boolean 
    If Char.IsLetter(Mid(TeacherUsername, 1, 3)) Then ' takes the first 3 characters of a user name to see if they are 
     ' letters 
     Valid1 = True 
    Else 
     Valid1 = False 
    End If 

    If Char.IsNumber(Mid(TeacherUsername, 4, 8)) Then 'does the same with numbers, starting at char(4) and taking 8. 
     Valid2 = True 
    Else 
     Valid2 = False 
    End If 
    If Valid1 = True And Valid2 = True Then 
     ValidTeacherUsernameCheck = True 
    Else 
     ValidTeacherUsernameCheck = False 
    End If 
    Return ValidTeacherUsernameCheck 
End Function 

Function ValidPasswordCheck(ByRef ValidPassword As Boolean, ByVal Password As String) As Boolean 
    If System.Text.RegularExpressions.Regex.Match(Password, "\d").Success Then 
     ValidPasswordCheck = True 
    Else 
     ValidPasswordCheck = False 
    End If 
    Return ValidPasswordCheck 
End Function 

任何幫助將不勝感激。

+1

你爲什麼打電話validusernamecheck兩次?還傳遞一個布爾值按地址,但這似乎沒有得到根本 – pm100

+0

該行註釋使用'」需要一個用戶名的前3個字符,看看他們是letters'和類似不這樣做你認爲他們做的。請參閱[Char.IsLetter]文檔(https://msdn.microsoft.com/en-us/library/system.char.isletter%28v=vs.110%29.aspx) - 「Unicode字符」表示*一個*字符。 –

+0

使用這些函數的正確方法是'ValidUserName = ValidUsernameCheck(Username)'然後實際上對變量做些什麼。當然密碼永遠不應該作爲明文存儲。如果發生異常,您只能關閉連接,但我已經向您展示了使用連接的正確方法。 – Plutonix

回答

0

您的代碼似乎有點太複雜了。您可以在任何時候通過Return聲明從函數返回 - 只要您檢測到輸入值不正確,您可以Return False,因爲通常不需要進一步的驗證檢查。

看起來至少有一些熟悉的正則表達式,所以你可以使用一個檢查用戶名和密碼。

代碼出現學生被設置憑據,所以在讓用戶知道哪個條目有一個問題,如果有任何傷害。此外,它是一個好主意,告訴什麼格式的進入應該是在用戶

要檢查的ID,而不是名稱 - 你應該適當命名的功能。

所以,你的代碼看起來是這樣的:

Private Function IsIdFormatCorrect(ID As String) As Boolean 
    If String.IsNullOrEmpty(ID) OrElse ID.Length <> 11 Then 
     Return False 
    End If 

    ' require name to be exactly (three letters followed by eight digits) 
    Return Regex.IsMatch(ID, "^[A-Za-z]{3}[0-9]{8}$") 

End Function 

Private Function IsPasswordFormatCorrect(password As String) As Boolean 
    If String.IsNullOrEmpty(password) Then 
     Return False 
    End If 

    ' require password to be only digits and at least four of them 
    Return Regex.IsMatch(password, "^[0-9]{4,}$") 

End Function 

Private Sub bnSave_Click(sender As Object, e As EventArgs) Handles bnSave.Click 
    Dim errorText As String = "" 
    If Not IsIdFormatCorrect(txtStudentID.Text) Then 
     errorText = "Student ID not in correct format (""ABC12345678"")." & vbCrLf 
    End If 
    If Not IsIdFormatCorrect(txtTeacherID.Text) Then 
     errorText &= "Teacher ID not in correct format (""ABC12345678"")." & vbCrLf 
    End If 
    If Not IsPasswordFormatCorrect(txtStudentPassword.Text) Then 
     errorText &= "Student password not in correct format (at least four digits)." 
    End If 

    If errorText.Length > 0 Then 
     MessageBox.Show(errorText, "Data entry problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
    Else 
     ' save to database 
    End If 

End Sub 
+0

謝謝,這看起來比我的效率更高。 – User59