Solved

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

Posted on 2004-09-25
7
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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