Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-09-25
7
Medium Priority
?
225 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 86

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

581 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