Validating Input Text... (EASY FOR MOST!)

I have an input that I wish to validate.  Say the input is a textbox call txtInput.  When the user hits a button, I want to run a sub to validate it.  If it's a string, I want to send "Please enter a number" to a label called lblOutput.  if the number is negitive I want to send "Please enter a positive number" to the same label.  Can someone out there help me validate this textbox? THANK YOU.
Who is Participating?
GohdanTheMoblinConnect With a Mentor Commented:
The LostFocus event will occur when the user moves the cursor from the textbox, this may be useful.  Also, I converted without using exceptions because they seem to cause noticable performance drops on slower computers in my experience.

   Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        If IsNumeric(TextBox1.Text) Then
            'Check if it is numeric
            If CDbl(TextBox1.Text) < 0 Then
                'if it is, look if it is negative
                Label1.Text = "Negative"
                'do whatever you do when the text is valid here
            End If
            'Not numeric
            Label1.Text = "Not numeric"
        End If
    End Sub
Éric MoreauSenior .Net ConsultantCommented:
You better validate each character that is pressed:

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
                'OK valid character
            Case ControlChars.Back
                'OK backspace
            Case Else
                e.Handled = True
        End Select
    End Sub
MoonbathingAuthor Commented:
Thanx, but I was wondering if there's a way to do it after the user inputs the text.  Logically, it can go three ways.  The text can be ok (a postive number) and the label will be not visable, the input text can be a negitive and we will send some text to a label (I do not want to process tha calculation) or the input text could have letters in it and we'll send a some different text to the label (again without continuing and processing the calcuation)

Please help!
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one way to do it.  If you are looking for an Integer value, then replace Double with Integer everywhere you see it in the code.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dbl As Double
            dbl = Double.Parse(txtInput.Text)
            If dbl < 0 Then
                lblOutput.Text = "Please enter a positive number."
                lblOutput.Text = "Valid number entered"
            End If
        Catch ex As Exception
            lblOutput.Text = "Please enter a number."
        End Try
    End Sub


how about using numericupdown component and set the max and min value you want?
This event is fired everytime the user try to leave the focus of the textbox.

If you set e.Cancel = True then the user won't be able to leave the textbox.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If NoValidText() then
            e.Cancel = True
            e.Cancel = False

end sub

sub NoValidText()
     'Code to validate the text
end sub
it should be:

function NoValidText() as boolean
     'Code to validate the text
end function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.