English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

I have a text box how can i not allow special characters to be typed in the textbox

2007-07-11 18:34:12 · 4 answers · asked by y0ger_19 4 in Computers & Internet Programming & Design

The e.Handled doesn't allow anything to be typed in the line

2007-07-11 19:11:45 · update #1

4 answers

You can always strip HTML entities via a regular expression or the Replace method.

Dim myRe As New RegEx("(<)(>)(&)");
Dim myString As String = "Hello World!"
myString = myRe.Replace(myString, "")

2007-07-11 19:20:42 · answer #1 · answered by Anonymous · 0 1

this works well.
what you need:
an errorprovider named:errorprovider1
a TextBox named: TextBox1
An imports statement: Imports system.text.regularexpressions

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ErrorProvider1.SetError(TextBox1, "")
Dim matches As MatchCollection
Dim x As String = CType(sender, TextBox).Text.ToString

matches = Regex.Matches(x, "[^A-Za-z0-1\s]{1,}")
If matches.Count <> 0 Then 'an invalid key was entered or invalid text was pasted. remove it
For Each m As Match In matches
textbox1.Text =TextBox1.Text.Replace(m.ToString, "") 'replace the invalid character with nothing

Next
ErrorProvider1.SetError(TextBox1, "You may enter only values A-Z, a-z 0-9 and spaces")
TextBox1.SelectionStart = TextBox1.Text.Length 'set the cursor to the end of the text
End If
End Sub

A regular expression is a basically a pattern that is searched for in the defined text:

matches = Regex.Matches(x, "[^A-Za-z0-1\s]{1,}")

here is a quick breakdown: the ^ character means that it will search for a pattern that IS NOT A-Za-z0-9\s: \s is a space.
If a non-A-Za-z0-9\s pattern is found then the user has entered an invalid key or pasted characters with invalid characters and an error to the error provider is thrown.

The matches variable is set to a value if an unwanted character is found.

Feel free to contact me if you need to add more allowed characters to your text box.

Good question. This was a nice little project. Hope it works well for you

YIM: ebred

Good luck!!

2007-07-15 11:54:17 · answer #2 · answered by ebred 3 · 0 0

I'm assuming you are making a Windows Application, not a web application.

In the KeyPress event, any key that you don't want typed in the TextBox control, you can set e.Handled = True. That will prevent the key from being typed into the TextBox.

2007-07-11 18:43:38 · answer #3 · answered by Michael M 6 · 0 2

Well if its VB.net regarding asp.net you can use something like the regularexpressionvalidator, but if you want a more in real time approach you can attach events that monitors a textbox in javascript.

If its windows programming, you'll have to use regular expressions to validate your input, [a-zA-Z0-9] for example will only allow upper/lower case numbers and numerals - or you can have the same approach to attach events to your input to monitor input and simply test input against a regular expression (Regex class)

2007-07-11 19:10:23 · answer #4 · answered by cstruter 2 · 0 1

fedest.com, questions and answers