VB .Net : how to validate textbox to get only alphabets a-z , A-Z...Pls help me

Recommended Answers

All 7 Replies

to valid textboxes write the code in keypress event
like

private sub textbox1_keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles  textbox1.KeyPress
Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)
        Select Case KeyAscii
case 65 to 90 ''for capital ALPHABET A
case 97 to122
case else
keyascii=0
End Select

        If KeyAscii = 0 Then
            e.Handled = True
            MessageBox.Show("Please Type Word  only", "Type the Numbers!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            e.Handled = False
        End If

end sub

it's simple
try it will run

thank you..it is working as i expected...But when i press backspace or delete key, it is showing the messagebox. I want this textbox to accept backspace and delete key also..please help me...

then add the KEYASCII value of enter and backspace
that is

case 13 
 case 8

in that code it will run if your problem is solved mark thread as a solve .

Thanks experts
it is useful thread for me.

The Below Code validation for Text,Numeric values and symbols are not allowed.

If Char.IsPunctuation(e.KeyChar) = True And Asc(e.KeyChar) <> 8 Then
            e.Handled = True
            MsgBox("Please Enter only letter.")
ElseIf Char.IsNumber(e.KeyChar) = True And Asc(e.KeyChar) <> 8 Then
            e.Handled = True
            MsgBox("Please Enter only letter.")
End If

For Symbol and Text

If Char.IsNumber(e.KeyChar) = True And Asc(e.KeyChar) <> 8 Then
            e.Handled = True
            MsgBox("Please Enter only letter.")
End If

Code For allowing Numbers only:

If (Char.IsPunctuation(e.KeyChar) = True Or Char.IsLetter(e.KeyChar) = True) And   Asc(e.KeyChar) <> 8 Then
            e.Handled = True
            MsgBox("Enter Numbers Only")
        End If
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        If Not IsNumeric(.Text) Then
            MessageBox.Show("please enter a valid string", "Error" _
                             , MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.