Results 1 to 6 of 6

Thread: [RESOLVED] Validation on multiple fields?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    26

    [RESOLVED] Validation on multiple fields?

    Im trying to validate all the fields before I enable the calcButton. The issue I'm having is that the errorMessage doesn't clear once i enter a correct value. Hence the calcButton is disabled. How can I clear the ErrorProvider once the validation is good?

    This code is probably not the write way to do it. Any advice on a better way to code it would be greatly appreciated, and if you have some sample coding that would be icing on the cake.

    Thanks again.

    VB Code:
    1. Private Sub SetFormErrorState()
    2.         Dim errorMessage As String = Nothing
    3.         'Checking CC# for numeric
    4.         If Not Me.ValidateTextBox(Me.phoneTextBox.Text) Then
    5.             'The user has not entered the required value.
    6.             errorMessage = "Entry Required"
    7.             'Checking phone for numeric
    8.             Me.ErrorProvider.SetError(Me.phoneTextBox, errorMessage)
    9.  
    10.         ElseIf Me.ccTextBox.Enabled AndAlso Not Me.ValidateTextBox(Me.ccTextBox.Text) Then
    11.             'The user has not entered the required value.
    12.             errorMessage = "Entry Required"
    13.             Me.ErrorProvider.SetError(Me.ccTextBox, errorMessage)
    14.  
    15.         ElseIf Not Me.ValidateTextBox(Me.startMiTextBox.Text) Then
    16.             'The user has not entered the required value.
    17.             errorMessage = "Entry Required"
    18.             Me.ErrorProvider.SetError(Me.startMiTextBox, errorMessage)
    19.  
    20.         ElseIf Not Me.ValidateTextBox(Me.endMiTextBox.Text) Then
    21.             'The user has not entered the required value.
    22.             errorMessage = "Entry Required"
    23.             Me.ErrorProvider.SetError(Me.endMiTextBox, errorMessage)
    24.  
    25.         ElseIf Not Me.ValidateTextBox(Me.daysRentedTextBox.Text) Then
    26.             'The user has not entered the required value.
    27.             errorMessage = "Entry Required"
    28.             Me.ErrorProvider.SetError(Me.daysRentedTextBox, errorMessage)
    29.         Else
    30.             Me.ErrorProvider.Clear()
    31.         End If
    32.  
    33.         'Enable the OK button if and only if there are no errors.
    34.         Me.calculateButton.Enabled = (errorMessage Is Nothing)
    35.     End Sub
    36.  
    37.     Private Function ValidateTextBox(ByVal varTextBoxString As String) As Boolean
    38.         If varTextBoxString = "" Then
    39.             Return False
    40.         Else
    41.             Return True
    42.         End If
    43.         'Return True if the TextBox contains any value, otherwise return False.
    44.     End Function
    Last edited by mrman99; Sep 19th, 2006 at 10:30 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Validation on multiple fields?

    You need to handle the error for each TextBox separately and each time the text changes. This works as you'd expect:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Me.SetErrorState()
    3. End Sub
    4.  
    5. Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, _
    6.                                                                                                     TextBox2.TextChanged, _
    7.                                                                                                     TextBox1.TextChanged
    8.     Me.SetErrorState()
    9. End Sub
    10.  
    11. Private Sub SetErrorState()
    12.     Const ERR_MSG As String = "Required field"
    13.     Dim errCount As Integer = 0
    14.  
    15.     If Me.ValidateNonEmptyString(Me.TextBox1.Text) Then
    16.         Me.ErrorProvider1.SetError(Me.TextBox1, Nothing)
    17.     Else
    18.         Me.ErrorProvider1.SetError(Me.TextBox1, ERR_MSG)
    19.         errCount += 1
    20.     End If
    21.  
    22.     If Me.ValidateNonEmptyString(Me.TextBox2.Text) Then
    23.         Me.ErrorProvider1.SetError(Me.TextBox2, Nothing)
    24.     Else
    25.         Me.ErrorProvider1.SetError(Me.TextBox2, ERR_MSG)
    26.         errCount += 1
    27.     End If
    28.  
    29.     If Me.ValidateNonEmptyString(Me.TextBox3.Text) Then
    30.         Me.ErrorProvider1.SetError(Me.TextBox3, Nothing)
    31.     Else
    32.         Me.ErrorProvider1.SetError(Me.TextBox3, ERR_MSG)
    33.         errCount += 1
    34.     End If
    35.  
    36.     Me.Button1.Enabled = (errCount = 0)
    37. End Sub
    38.  
    39. Private Function ValidateNonEmptyString(ByVal str As String) As Boolean
    40.     Return str.Trim().Length > 0
    41. End Function
    There are ways to reduce the code but that's the general idea. The more TextBoxes that have different validation requirements, such as the one that might be disabled, the less you could reduce the code.
    Last edited by jmcilhinney; Sep 19th, 2006 at 08:41 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    26

    Re: Validation on multiple fields?

    VB Code:
    1. Private Sub PaymentTypeComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PaymentTypeComboBox.SelectedIndexChanged
    2.         'disable the ccTextBox if "Cash" is selected
    3.         Me.ccTextBox.Enabled = (CStr(Me.PaymentTypeComboBox.SelectedItem) <> "Cash")
    4.     End Sub
    5.  
    6.     Private Sub ccTextBox_PropertyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ccTextBox.EnabledChanged, ccTextBox.TextChanged
    7.         'Clear the TextBox if it is disabled.
    8.         If Not Me.ccTextBox.Enabled Then
    9.             Me.ccTextBox.Clear()
    10.         End If
    11.  
    12.         'Set the properties of the OK button and ErrorProvider appropriately for the state of the TextBox.
    13.         Me.SetFormErrorState()
    14.     End Sub
    15. Private Sub SetFormErrorState()
    16.         Const ERR_MSG As String = "Entry Required"
    17.         Dim errCount As Integer = 0
    18.  
    19.  
    20.         If Me.ValidateNonEmptyString(Me.phoneTextBox.Text) Then
    21.             Me.ErrorProvider.SetError(Me.phoneTextBox, Nothing)
    22.         Else
    23.             Me.ErrorProvider.SetError(Me.phoneTextBox, ERR_MSG)
    24.             errCount += 1
    25.         End If
    26.  
    27.         If Me.ccTextBox.Enabled AndAlso Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
    28.             Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
    29.         Else
    30.             Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
    31.             errCount += 1
    32.         End If
    33.  
    34.         If Me.ValidateNonEmptyString(Me.startMiTextBox.Text) Then
    35.             Me.ErrorProvider.SetError(Me.startMiTextBox, Nothing)
    36.         Else
    37.             Me.ErrorProvider.SetError(Me.startMiTextBox, ERR_MSG)
    38.             errCount += 1
    39.         End If
    40.  
    41.         If Me.ValidateNonEmptyString(Me.endMiTextBox.Text) Then
    42.             Me.ErrorProvider.SetError(Me.endMiTextBox, Nothing)
    43.         Else
    44.             Me.ErrorProvider.SetError(Me.endMiTextBox, ERR_MSG)
    45.             errCount += 1
    46.         End If
    47.  
    48.         If Me.ValidateNonEmptyString(Me.daysRentedTextBox.Text) Then
    49.             Me.ErrorProvider.SetError(Me.daysRentedTextBox, Nothing)
    50.         Else
    51.             Me.ErrorProvider.SetError(Me.daysRentedTextBox, ERR_MSG)
    52.             errCount += 1
    53.         End If
    54.  
    55.         If errCount = 0 Then
    56.             Me.ErrorProvider.Clear()
    57.         End If
    58.  
    59.         'Enable the OK button if and only if there are no errors.
    60.         Me.calculateButton.Enabled = (errCount = 0)
    61.     End Sub

    everything works great, but when the ccTextBox is disabled, the error still shows up "Entry Required!"

    VB Code:
    1. If Me.ccTextBox.Enabled AndAlso Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
    2.             Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
    3.         Else
    4.             Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
    5.             errCount += 1
    6.         End If
    Is this part look right?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Validation on multiple fields?

    No it doesn't. Ask yourself, when do you want to display an error on ccTextBox and when do you not, then ask yourself whether the condition you've used will do that. Where will that code go if the Enabled property is False?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    26

    Re: Validation on multiple fields?

    VB Code:
    1. If Me.ccTextBox.Enabled AndAlso Not Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
    2.             Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
    3.             errCount += 1
    4.         Else
    5.             Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
    6.         End If

    changed it to the above to make it work right

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Validation on multiple fields?

    Not that there's anything wrong with that but for future reference note that the following would also work:
    VB Code:
    1. If Not Me.ccTextBox.Enabled OrElse Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
    2.             Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
    3.         Else
    4.             Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
    5.             errCount += 1
    6.         End If
    If you weren't going to use an Else block then that might be preferable. As you are using an Else block neither is any better than the other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width