This module is for validating textboxes....
After using this module in your project you can simply validate all of your text boxes like
Imports System.Text.RegularExpressions
Module Validator
Public Enum ValidationType
Only_Digits = 1
Only_Characters = 2
No_Blank = 3
Only_Email = 4
End Enum
Public Sub AssignValidation(ByRef CTRL As TextBox, ByVal Validation_Type As ValidationType)
Dim txt As TextBox = CTRL
Select Case Validation_Type
Case ValidationType.Only_Digits
AddHandler txt.KeyPress, AddressOf ONUM_Leave
Case ValidationType.Only_Characters
AddHandler txt.KeyPress, AddressOf OCHAR_Leave
Case ValidationType.No_Blank
AddHandler txt.Leave, AddressOf NOBLANK_Leave
Case ValidationType.Only_Email
AddHandler txt.Leave, AddressOf Email_Leave
End Select
End Sub
Public Sub ONUM_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim txt As TextBox = sender
If InStr("1234567890.", e.KeyChar) = 0 And Asc(e.KeyChar) <> 8 Or (e.KeyChar = "." And InStr(txt.Text, ".") > 0) Then
e.KeyChar = Chr(0)
e.Handled = True
End If
End Sub
Public Sub OCHAR_Leave(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If InStr("1234567890!@#$%^&*()_+=-", e.KeyChar) > 0 Then
e.KeyChar = Chr(0)
e.Handled = True
End If
End Sub
Public Sub NOBLANK_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim t As TextBox = sender
If t.Text.Trim = "" Then
MsgBox("This field Must be filled!")
t.Focus()
End If
End Sub
Public Sub Email_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtEmail_1 As TextBox = sender
If txtEmail_1.Text <> "" Then
Dim rex As Match = Regex.Match(Trim(txtEmail_1.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
If rex.Success = False Then
MessageBox.Show("Please Enter a valid Email-Address 1", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtEmail_1.BackColor = Color.Red
txtEmail_1.Focus()
Exit Sub
Else
txtEmail_1.BackColor = Color.White
End If
End If
End Sub
End Module
After using this module in your project you can simply validate all of your text boxes like
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AssignValidation(Me.TextBox1, ValidationType.Only_Digits)
AssignValidation(Me.TextBox2, ValidationType.Only_Characters)
AssignValidation(Me.TextBox3, ValidationType.No_Blank)
AssignValidation(Me.TextBox4, ValidationType.Only_Email)
End Sub
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
-
TextBox Validation Is So Easyon Feb 28 2012 04:58 PM
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|