Wednesday 25 January 2012

code for textbox validation in vb.net


Here the code for text box validation.
1 step
First create a new class and give name following _InputValidation
2 step
Now  create a new object of ________ .
How to use these function.
For use of this function call this function on textbox keyperess event,
Example of it

Dim InputValidation As New _InputValidation
    Private Sub OnlyCharecter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtAddress1.KeyPress

        InputValidation.OnlyCharecter(e)
    End Sub
    Private Sub OnlyNumber(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtAddress1.KeyPress

        InputValidation.OnlyInteger(e)
    End Sub
    Private Sub GenralInput(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtAddress1.KeyPress

        InputValidation.GenralInput(e)
    End Sub



Public Class _InputValidation
    Public Sub OnlyInteger(ByVal id As Object)
        'For textbox keypress validation for only input numeric Only
        If (Asc(id.KeyChar) < 48 Or Asc(id.KeyChar) > 57) And Asc(id.KeyChar) <> 8 And Asc(id.KeyChar) <> 58 Then
            id.Handled = True
        End If
    End Sub
    Public Sub OnlyDecimal(ByVal id As Object)
        'for textbox keypress validation for only input decimal
        If (Asc(id.KeyChar) < 48 Or Asc(id.KeyChar) > 57) And Asc(id.KeyChar) <> 8 And Asc(id.KeyChar) <> 58 And Asc(id.KeyChar) <> 46 Then
            id.Handled = True
        End If
    End Sub
    Public Sub OnlyCharecter(ByVal name As Object)
        'For textbox keypress validation for only input (A-Z)(a-z),Space,Back Space
        If (Asc(name.KeyChar) < 65 Or Asc(name.KeyChar) > 90) And (Asc(name.KeyChar) < 97 Or Asc(name.KeyChar) > 122) And Asc(name.KeyChar) <> 8 And Asc(name.KeyChar) <> 32 Then
            name.Handled = True
        End If
    End Sub
    Public Sub GenralInput(ByVal itnm As Object)
        'For textbox keypress validation for only input Numerice,(A-Z)(a-z),Space,Back Space,Dots
        If (Asc(itnm.KeyChar) < 65 Or Asc(itnm.KeyChar) > 90) And (Asc(itnm.KeyChar) < 97 Or Asc(itnm.KeyChar) > 122) And (Asc(itnm.KeyChar) < 48 Or Asc(itnm.KeyChar) > 57) And Asc(itnm.KeyChar) <> 46 And Asc(itnm.KeyChar) <> 8 And Asc(itnm.KeyChar) <> 32 Then
            itnm.Handled = True
        End If
    End Sub
End Class



No comments:

Post a Comment