Subscribe to kingsonprisonic's Blog        RSS Feed
-----

TextBox Validation Is So Easy

Icon Leave Comment
This module is for validating textboxes....

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 ]

There are no Trackbacks for this entry

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Recent Entries

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)

    Categories