文本框验证:仅允许使用字母和简单括号"("和")" vb.net

时间:2017-02-03 05:14:29

标签: vb.net-2010

我想用keyPress事件验证文本框。它应该允许字母和"("和")"我已经编写了字母表检查代码,但不知道如何检查"("和")"。

     Private Sub txtBankName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtBankName.KeyPress
    If Not (Asc(e.KeyChar) = 8) Then
        If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90) Or Asc(e.KeyChar) = 32) Then
            e.KeyChar = ChrW(0)
            e.Handled = True
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

这允许所有字母和&#34;(&#34;&amp;&#34;)&#34;。请参阅这两个SO问题:
How can I validate a string to only allow alphanumeric characters in it?
Only allow specific characters in textbox

Private Sub txtBankName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtBankName.KeyPress    
    If System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[^a-zA-Z0-9()\b]") Then
       e.Handled = True
    End If
End Sub