Validate textbox

tpistol

Member
Joined
Nov 18, 2012
Messages
5
Programming Experience
Beginner
Hello - I'm extremely new to VB but appreciate any help you can offer.

I am trying to validate input in a textbox where a GPA is entered. I want to give the user an error message when he/she does not enter anything or enters a number that is not between 0 and 4. I have done a keypress event to limit the characters that can be entered to numbers, backspace, and the period and this is working fine.

I'm doing this as a function and this is my code so far:

Function IsGoodInput(ByVal number As String) As Boolean
If txtgpa.Text = "" Or Val(txtgpa.Text) > 4 Then
MsgBox("Please enter a number between 0 and 4.", "Input error.")
txtgpa.Focus()
Return False
Else
Return True
End If

I've tried this a few ways and I either get a conversion error or an invalid cast error. Thank you for any assistance.
 
Why aren't you using the NumericUpDown control?
 
I have to concur with JohnH but, if you're determined to use a TextBox, here's how. Firstly, you need to decide on prevention or cure. If you choose prevention then you should be handling the KeyDown event so that you can stop any invalid data ever being entered into the control. If you choose cure then you should be handling the Validating event, which will prevent the user leaving the control if it contains invalid data.

Also, I recommend never using Val. You should preferentially use the TryParse method of the numeric type that you want to convert to, e.g. Double.TryParse.
 
Here is a little example that I threw together that uses the TextBox.Validating event to check the input and if there is a problem, it uses an ErrorProvider to display a relevant message. Notice that I've also handled the TextBox.Validated event which is needed to clear the ErrorProvider if it was previously displayed due to invalid input.

VB.NET:
Public Class Form1

    Private Sub TextBox1_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        Dim errorMessage As String = String.Empty


        If Not Me.ValidGPA(Me.TextBox1.Text, errorMessage) Then
            ' Cancel the validating event
            e.Cancel = True


            ' Set the error provider message
            Me.ErrorProvider1.SetError(Me.TextBox1, errorMessage)
        End If




    End Sub


    Private Sub TextBox1_Validated(sender As System.Object, e As System.EventArgs) Handles TextBox1.Validated


        ' Clear the error provider when a valid gpa is input
        Me.ErrorProvider1.SetError(Me.TextBox1, String.Empty)


    End Sub


    Private Function ValidGPA(gpaInput As String, ByRef errorMessage As String) As Boolean


        ' Check for blank input
        If gpaInput.Length = 0 Then
            errorMessage = "GPA input is required"
            Return False
        End If


        ' Check the input value
        Dim gpa As Double
        If Double.TryParse(gpaInput, gpa) Then
            If gpa < 0 OrElse gpa > 4 Then
                errorMessage = "GPA input must be between 0 and 4"
                Return False
            End If
        Else
            errorMessage = "GPA input is not valid"
            Return False
        End If


        Return True


    End Function


End Class

Here is a capture of what the ErrorProvider displays on the form.

ErrorProvider.png

There are other approaches to handling the validation issue. In some cases, you may not want or be able to validate each input as it is filled in on a form, in which case you could do validation for the entire form when the user tries to submit the input. This is especially the case where certain inputs are dependent on other inputs on the same form.
 
Home Grown Coder a version of what you posted did the trick...many thanks to you and all who responded. The way I coded is how our prof instructed us.
 
Back
Top