vbnetprogrammer
vbnetprogrammer

Reputation: 33

Textbox only letters, numbers, and -

Hi i am trying to check whether user inputs within textbox was only letters, digitis and - symobol. After he typed and click button this should be checked. So far i did that below. Could you please tell me whether is that correct way? What is missing here is - symbol to be allowed.

For Each c As Char In TextBox1.Text
    If Not Char.IsLetterOrDigit(c) Then
        MsgBox "Only letters, digits and - symbol is allowed !"
        Exit For
    Else
        MsgBox "OK"
    End If
Next

Upvotes: 1

Views: 112

Answers (1)

TyCobb
TyCobb

Reputation: 9089

If Not Char.IsLetterOrDigit(c) AndAlso c <> "-"c Then

c after quotes says that you want it to be Char.

There's nothing wrong doing what you have. Some will say, "use RegEx!". You can, but it is not necessary here and I prefer the readability of what you have over RegEx.

Upvotes: 3

Related Questions