Solved

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

Posted on 2004-09-25
7
215 Views
Last Modified: 2010-04-23
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.
0
Comment
Question by:Moonbathing
7 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 12150833
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
                Beep()
                e.Handled = True
        End Select
    End Sub
0
 

Author Comment

by:Moonbathing
ID: 12150884
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!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12151011
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
        Try
            Dim dbl As Double
            dbl = Double.Parse(txtInput.Text)
            If dbl < 0 Then
                lblOutput.Text = "Please enter a positive number."
            Else
                lblOutput.Text = "Valid number entered"
            End If
        Catch ex As Exception
            lblOutput.Text = "Please enter a number."
        End Try
    End Sub

Regards,

Idle_Mind
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:tzxie2000
ID: 12151030
how about using numericupdown component and set the max and min value you want?
0
 
LVL 2

Accepted Solution

by:
GohdanTheMoblin earned 500 total points
ID: 12151238
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"
            Else
                'do whatever you do when the text is valid here
            End If
        Else
            'Not numeric
            Label1.Text = "Not numeric"
        End If
    End Sub
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12151782
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
       else
            e.Cancel = False
       endif

end sub

sub NoValidText()
     'Code to validate the text
end sub
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12151785
it should be:

function NoValidText() as boolean
     'Code to validate the text
end function
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now