You are on page 1of 3

For alphanumeric textbox

Private Sub txtfindWhat_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtfindWhat.TextChanged Dim sText As String Dim cInvalidChars() As Char = {"~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "=", "+", "{", "[", "}", "]", "|", "\", ":", ";", """", "'", "<", ",", ">", ".", "?", "/"} Dim iIndex As Integer sText = txtfindWhat.Text iIndex = sText.IndexOfAny(cInvalidChars) 'All valid chars If (iIndex = -1) Then Return 'Remove invalid char txtfindWhat.Text = sText.Remove(iIndex, 1) 'Put the cursor back on the last pos txtfindWhat.SelectionStart = txtfindWhat.Text.Length End Sub

For numeric textbox


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e AsSystem. Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then e.Handled = True MessageBox.Show("Please enter only number") End If End Sub End Class

Regular expression symbols


SYMBOL \ . ^ NAME backslash period carat DESCRIPTION Turn off the special meaning of the next character, as in \^. Match a single character of any value, except end of line. Match the start of the line, as in ^A.

$ | [] [^]

dollar sign pipe square brackets carat enclosed in square brackets parentheses exclamation point question mark asterisk plus n enclosed in braces n, enclosed in braces ,n enclosed in braces n,m enclosed in braces

Match the end of the line, as in A$. Match either the regular expression preceding it or the regular expression following it. Match any single character from within the bracketed list, as in [abcde]. Use a hyphen (-) for a range, as in [0-9]. Within square brackets, most characters are interpreted literally. Match any single character except those within the bracketed list, as in [^0-9]. Group one or more regular expressions to establish a logical regular expression consisting of sub-regular expressions. Used to override the standard precedence of specific operators. Do not match the next character or regular expression. Match 0 or 1 time. Match 0 or more times. Match 1 or more times. Match exactly n times. Match n or more times. Match n or fewer times. Match at least n times but not more than m times.

() ! ? * + {n} {n,} {,n} {n,m}

For alphabetic textbox only


1. Private Sub textDate_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles textDate.KeyPress

2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _ 3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _ 4. And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _ 5. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then 6. 'space accepted 7. If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then 8. e.Handled = True 9. End If 10. End If 11. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then 12. 13. e.Handled = False 14. End If 15. End Sub

You might also like