0% found this document useful (0 votes)
18 views2 pages

Introduction

This document provides code to validate user input in text boxes. It includes code to handle the click event of two buttons and keypress events of two text boxes. The code for the first text box and button only allows letters and spaces, while the code for the second only allows digits. Validation messages are displayed if blank or invalid characters are entered.

Uploaded by

fancybala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Introduction

This document provides code to validate user input in text boxes. It includes code to handle the click event of two buttons and keypress events of two text boxes. The code for the first text box and button only allows letters and spaces, while the code for the second only allows digits. Validation messages are displayed if blank or invalid characters are entered.

Uploaded by

fancybala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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:

 Collapse
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

You might also like