Results 1 to 11 of 11

Thread: How to Validate Textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    10

    Question How to Validate Textbox

    How to validate the textbox?If empty return error message

  2. #2
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: How to Validate Textbox

    VB Code:
    1. If TextboxName.text = "" Then
    2.      MessageBox.Show("Please enter something")
    3. End If

  3. #3
    Member
    Join Date
    Mar 2005
    Posts
    40

    Re: How to Validate Textbox

    Code:
    if textbox1.text="" then
    msgbox("invalid")
    end if

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: How to Validate Textbox

    it's a good idea to put the focus back on the textbox, and also you need those code in the Validated event of your textbox. Use the Trim function to remove empty space also (so if they enter " " it would be considered "")

    VB Code:
    1. Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
    2.         If TextBox1.Text.Trim = "" Then
    3.             TextBox1.Focus()
    4.             MessageBox.Show("Boo!")
    5.         End If
    6.     End Sub
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    10

    Question Re: How to Validate Textbox

    If want to validate more than 1 text box can i include inside the sub procedure?

  6. #6
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: How to Validate Textbox

    VB Code:
    1. Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles [u]TextBox1.Validated, TextBox2.Validated, TextBox3.Validated[/u]
    2.         Dim t As TextBox = CType(sender, TextBox)
    3.         If t.Text = "" Then
    4.             t.Focus()
    5.             MessageBox.Show("Boo!")
    6.         End If
    7.     End Sub
    Just an addition to the not polite guy.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    10

    Smile Re: How to Validate Textbox

    Hi,

    I didn't understand this part
    Dim t As TextBox = CType(sender, TextBox)

    Thank You

  8. #8
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: How to Validate Textbox

    sender object (the one who sends a message to the event) is casted to it's TextBox form. Then pass the instance to t which of type TextBox. Then we can manipulate t, ie. display the TextBox's Name property or Tag, etc.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    10

    Unhappy Re: How to Validate Textbox

    Hi,

    Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated, TextBox3.Validated
    Dim t As TextBox = CType(sender, TextBox)
    If t.Text = "" Then
    t.Focus()
    MessageBox.Show("Boo!")
    End If
    End Sub

    This code just validates only the first field

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    10

    Unhappy Re: How to Validate Textbox

    Hi,

    Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated, TextBox3.Validated
    Dim t As TextBox = CType(sender, TextBox)
    If t.Text = "" Then
    t.Focus()
    MessageBox.Show("Boo!")
    End If
    End Sub

    This code just validates only the first field

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How to Validate Textbox

    Hi,

    The it depends on what you want to validate. If you want to check each keypress for a valid number see the codebank

    http://www.vbforums.com/showthread.php?t=314936

    If you want to check for something other than numerics you should be able to adapt that code.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width