Friday, June 27, 2008

Text Validation (vb.net)

'for valid text only in textbox
Private Sub txtCountry_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCountry.KeyPress
Call KeyPressEventTF(e.KeyChar, e)
End Sub


Public Sub KeyPressEventTF(ByVal ekeychar As String, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Dim valid As Int32 = CharValid(ekeychar)
If valid = 0 Then
e.Handled = False
End If
If valid = 1 Then
e.Handled = True
End If
End Sub


Public Function CharValid(ByVal key_char As String) As Int32
If (Microsoft.VisualBasic.Asc(key_char) <> 90) _
And (Microsoft.VisualBasic.Asc(key_char) <> 122) Then
'Allowed space
If (Microsoft.VisualBasic.Asc(key_char) <> 32) Then
'e.Handled = True
handel1 = True
End If
End If
' Allowed backspace
If (Microsoft.VisualBasic.Asc(key_char) = 8) Then
'e.Handled = False
handel1 = False
End If
If handel1 = True Then
Return 1
Else
Return 0
End If
End Function

1 comment:

darichkid said...

This is an excellent library for verifying email addresses:
http://www.kellermansoftware.com/p-37-net-email-validation.aspx