Solved

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

Posted on 2004-09-25
7
218 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 70

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

11 Experts available now in Live!

Get 1:1 Help Now