2016-10-10 36 views
0

我想跳過驗證的一部分,只是讓文本框不能有任何我不想要的東西。vb.net textbox沒有特殊字符

其意圖是允許輸入後退,空格和字母:d,r,i(上和下)。

我怎樣才能讓這個沒有特殊字符獲取進入像{},:;與一對夫婦如果塊的過濾數據」等

Private Sub txtParty_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParty.KeyPress 
    'allows only numbers, letter, space, and backspace 
    If Char.IsControl(e.KeyChar) = False And Char.IsSeparator(e.KeyChar) = False And Char.IsLetterOrDigit(e.KeyChar) = True And e.KeyChar <> "d" And e.KeyChar <> "D" And e.KeyChar <> "r" And e.KeyChar <> "R" And e.KeyChar <> "i" And e.KeyChar <> "I" Then 
     e.Handled = True 
    End If 
End Sub 
+0

你可以使用與客戶端驗證的正則表達式。這是ASP。淨W eb表單? – Win

+0

從MSDN向TextBox添加正則表達式驗證https://msdn.microsoft.com/zh-cn/library/ms996428.aspx –

回答

2

可能更容易!?。

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) 
           Handles TextBox1.KeyPress 
    If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> " " Then 
    If Not Char.IsLetter(e.KeyChar) OrElse 
     Not "DRI".Contains(e.KeyChar.ToString.ToUpper) Then 
     e.Handled = True 
    End If 
    End If 
End Sub 

當然,你仍然要攔截按Ctrl-V和刪除的ContextMenuStrip防止粘貼文本到文本框。