Click here to Skip to main content
Licence 
First Posted 10 Jul 2006
Views 27,113
Downloads 312
Bookmarked 20 times

How to validate text boxes simply

By | 10 Jul 2006 | Article
Thi sarticle explains simple and easy way to validate user input of text boxes

Introduction

This article explains simple ways to validate user input in a text box.

Here is the code; let's say we have two textboxes named txt1 and txt2 and two buttons btn1 and btn2. Add the code given below:

Private Sub btn1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btn1.Click

  If (txt1.Text.Trim = "") Then
    MsgBox("Blank not Allowed", MsgBoxStyle.Information, "Verify")
  Else
    MsgBox(txt1.Text, MsgBoxStyle.Information, "Verify")
  End If

  txt1.Clear()
  txt1.Focus()
End Sub

Private Sub txt1_KeyPress(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles txt1.KeyPress
  If (Char.IsControl(e.KeyChar) = False) Then
    If (Char.IsLetter(e.KeyChar)) Or (Char.IsWhiteSpace(e.KeyChar)) Then
      'do nothing
    Else
      e.Handled = True
      MsgBox("Sorry Only Character & Spaces Allowed!!", _
             MsgBoxStyle.Information, "Verify")
      txt1.Focus()
    End If
  End If
End Sub

Private Sub txt2_KeyPress(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles txt2.KeyPress
  If (Char.IsControl(e.KeyChar) = False) Then
    If (Char.IsDigit(e.KeyChar)) Then
      'do nothing
    Else
      e.Handled = True
      MsgBox("Sorry Only Digits Allowed!!", _
             MsgBoxStyle.Information, "Verify")
      txt2.Focus()
    End If
  End If
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles btn2.Click
  If (txt2.Text.Trim = "") Then
    MsgBox("Blank not Allowedt", MsgBoxStyle.Information, "Verify")
  Else
    MsgBox(txt2.Text, MsgBoxStyle.Information, "Verify")
  End If

  txt2.Clear()
  txt2.Focus()
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

sam_ten2001

Web Developer

India India

Member

i m a beginner

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIsn't there a rating bellow 1?! PinmemberItay Sagui20:54 10 Jul '06  
QuestionHow to validate text boxed in a complicated manner PinmemberEnnis Ray Lynch, Jr.8:22 10 Jul '06  
AnswerNah, that looks bad, try this one PinmemberEnnis Ray Lynch, Jr.8:36 10 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 10 Jul 2006
Article Copyright 2006 by sam_ten2001
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid