如何在文本框验证错误时停止插入?

时间:2011-11-29 02:15:50

标签: asp.net vb.net

我试过这样的东西,它插入数据库甚至认为nric是错误的。 所以我希望它在nric验证错误时停止将数据插入数据库,但是从我的工作中,结果是它仍然在...中插入名称所以应该更改以允许它停止插入,直到用户更改然后该值可以继续插入???

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        register()

    End Sub   

 Protected Sub nricValidate()
        Dim strRegex As String = "^([sS]\d{7}[a-zA-Z])$"
        Dim myRegex As Regex = New Regex(strRegex)
        Dim strNr As String = txtNRIC.Text
        Dim nric As String = txtNRIC.Text
        If String.IsNullOrEmpty(txtNRIC.Text) Then

        ElseIf myRegex.IsMatch(strNr) Then
            Dim nricArray() As Char = nric.ToArray
            Dim sum As Integer = 0
            Dim num As Integer = 0
            Dim result As Integer = 0
            Dim numbers As Char
            Dim no As String = ""

            Dim i As Integer = 0
            Do While (i < nricArray.Length)
                If (i = 1) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 2)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 2) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 7)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 3) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 6)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 4) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 5)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 5) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 4)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 6) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 3)
                    nricArray(i) = Convert.ToChar(num)
                ElseIf (i = 7) Then
                    num = 0
                    numbers = nricArray(i)
                    no = numbers.ToString
                    num = Convert.ToInt32(no)
                    num = (num * 2)
                    nricArray(i) = Convert.ToChar(num)
                End If
                i = (i + 1)
            Loop
            i = 0
            Do While (i < nricArray.Length)
                If ((i > 0) _
                            AndAlso (i < 8)) Then
                    numbers = nricArray(i)
                    num = Convert.ToInt32(numbers)
                    sum = (sum + num)
                End If
                i = (i + 1)
            Loop
            result = (sum Mod 11)
            If (result = 10) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(65)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'A' Nric Error"
                End If
            ElseIf (result = 9) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(66)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'B' Nric Error"
                End If
            ElseIf (result = 8) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(67)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'C'Nric Error"
                End If
            ElseIf (result = 7) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(68)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'D'Nric Error"
                End If
            ElseIf (result = 6) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(69)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'E'Nric Error"
                End If
            ElseIf (result = 5) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(70)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'F'Nric Error"

                End If
            ElseIf (result = 4) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(71)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'G'Nric Error"
                End If
            ElseIf (result = 3) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(72)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'H'Nric Error"
                End If
            ElseIf (result = 2) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(73)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'I'Nric Error"
                End If
            ElseIf (result = 1) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(90)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'Z'Nric Error"
                End If
            ElseIf (result = 0) Then
                If (nricArray(8) = Microsoft.VisualBasic.ChrW(74)) Then

                Else

                    txtNRIC.Focus()
                    ResultLabel.Text = "last alphabet should be 'J'Nric Error"
                End If
            End If



            Return
        Else
            ResultLabel.Text = "The NRIC is incorrect!"
            txtNRIC.Text = String.Empty
            txtNRIC.Focus()
        End If


    End Sub

Protected Sub register()

        Dim myConn As New SqlConnection
        Dim myCmd As New SqlCommand
        myConn.ConnectionString = ConfigurationManager.ConnectionStrings("Company").ConnectionString
        Dim cmd As String
        cmd = "Insert into Customer values (@fullName,  @nric) "
        myCmd.CommandText = cmd
        myCmd.CommandType = CommandType.Text

        nricValidate()

        myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text))

        myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text))


        myCmd.Connection = myConn
        myConn.Open()
        myCmd.ExecuteNonQuery()
        myCmd.Dispose()
        myConn.Dispose()




    End Sub

4 个答案:

答案 0 :(得分:3)

这个大循环完全没必要。这里有很多要解决的问题,我会做大改写。我把216行变成了41行,没问题。它也很可能会变得更好。

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If nricValidate() Then
        Using myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("Company").ConnectionString),
              myCmd As SqlCommand = myConn.CreateCommand()
            myCmd.CommandText = "INSERT INTO Customer VALUES(@fullName, @nric)"
            myCmd.CommandType = CommandType.Text

            myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text))
            myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text))

            myConn.Open()
            myCmd.ExecuteNonQuery()
        End Using
    End If
End Sub

Protected Function nricValidate() As Boolean
    Dim myRegex As New Regex("^([sS]\d{7}[a-zA-Z])$")

    If Not String.IsNullOrEmpty(txtNRIC.Text) AndAlso myRegex.IsMatch(txtNRIC.Text) Then
        Dim nricArray(txtNRIC.Text.Length - 1) As Integer
        Dim sum As Integer = 0

        For i As Integer = 1 To 7
            sum += Integer.Parse(txtNRIC.Text.Substring(i, 1)) * If(i = 1, 2, 9 - i)
        Next

        If nricArray(8) <> 75 - sum Mod 11 Then
            txtNRIC.Focus()
            ResultLabel.Text = "The last value should be " & (75 - sum Mod 11).ToString() & ": NRIC Error"
            Return False
        End If

        Return True
    Else
        ResultLabel.Text = "The NRIC is incorrect!"
        txtNRIC.Text = String.Empty
        txtNRIC.Focus()
    End If

    Return False
End Function

您的实际答案是 - 您需要将nricValidate转换为Function,返回成功值,并在插入数据库之前检查是否成功。但正如您所见,您的其余代码也可以进行大量优化。

我事先道歉,但这是我一生中见过的最糟糕的代码。请仔细阅读如何编程。

答案 1 :(得分:1)

如果验证通过

,请将nricValidate更改为true
Protected Function nricValidate() As Boolean
    'Return True if validation pass
End Function

然后你可以验证并继续

If nricValidate() Then
    Dim myConn As New SqlConnection
    Dim myCmd As New SqlCommand
    myConn.ConnectionString = ConfigurationManager.ConnectionStrings("Company").ConnectionString
    Dim cmd As String
    cmd = "Insert into Customer values (@fullName,  @nric) "
    myCmd.CommandText = cmd
    myCmd.CommandType = CommandType.Text
    myCmd.Parameters.Add(New SqlParameter("@fullName", txtName.Text))
    myCmd.Parameters.Add(New SqlParameter("@nric", txtNRIC.Text))
    myCmd.Connection = myConn
    myConn.Open()
    myCmd.ExecuteNonQuery()
    myCmd.Dispose()
    myConn.Dispose()
End If

另一种方法是,如果ResultLabel验证文本失败,请在执行数据库操作之前检查。

答案 2 :(得分:1)

每次验证失败时都必须return false。像这样:

    If (nricArray(8) = Microsoft.VisualBasic.ChrW(65)) Then

    Else

       txtNRIC.Focus()
       ResultLabel.Text = "last alphabet should be 'A' Nric Error"
       return False
    End If
...
    return True 'at the end of the function

别忘了将sub更改为布尔返回类型的函数。

Protected function nricValidate() as Boolean

然后在你注册sub

nricValidate()替换为if not nricValidate() then exit sub并在任何声明之前进行,以便不需要处置任何内容......

答案 3 :(得分:0)

由于您将错误发布到结果标签,因此您可以在按钮提交中使用此对象来检测错误:

If (String.IsNullOrEmpty(ResultLabel.Text)) Then
  ' valid,continue
End IF