Text box text validation

urvi_bhal

Member
Joined
Jun 29, 2005
Messages
14
Programming Experience
Beginner
hi,

I am validating text character in textbox control. I want to validate that in text box control only particular character should be entered. Like if someone enter
abc/abc than / is not a valid character. How can I do this?
thanks
 
The best way to achieve this depends on exactly what characters are allowed and which aren't. Is it just letters, letters and numbers, just certain letters, or something else?
 
While there are certainly other ways to accomplish this task this one is indeed one of them :)
VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] DisableKeys([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] Key [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Boolean
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] Key = 47 [/size][size=2][color=#0000ff]Or[/color][/size][size=2] Key = 92 [/size][size=2][color=#0000ff]Then[/color][/size][size=2][color=#008000]'disabled slashes / \
 
[/color][/size][size=2]DisableKeys = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]DisableKeys = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Function
 
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TextBox1_KeyPress([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.KeyPressEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TextBox1.KeyPress
 
e.Handled = DisableKeys(Asc(e.KeyChar))
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
[/color][/size]

Cheers ;)
 
Last edited:
what i do is put all the valid characters into a global string variable (in a module) as well as a global function of which i simply call the function from the textbox's keypress event:

this example is for dealing with names:
VB.NET:
Friend Const gstrValidChars As String = "abcdefghijklmnopqrstuvwxyz'- ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	Friend Sub KeyPressName(ByRef Sender As Object, ByRef e As System.Windows.Forms.KeyPressEventArgs, ByVal AllowAllChars As Boolean)
		Dim TextBox As TextBox = CType(Sender, TextBox)
		Dim strLastChar As String
		'Handles all the name fields in the app
		If Asc(e.KeyChar) <> Keys.Back Then
			If AllowAllChars = False And InStr(gstrValidChars, e.KeyChar) = 0 Then
			    'If an invalid char is passed then its dropped
				e.Handled = True
			Else
			    If e.KeyChar.IsLetter(e.KeyChar) = True Then
				    TextBox.SelectedText = ""
				    If TextBox.TextLength <> 0 Then
					    strLastChar = Mid(TextBox.Text, TextBox.TextLength, 1)
					Else
					    strLastChar = "'"
					End If
				    If strLastChar = " " Or strLastChar = "-" Or strLastChar = "'" Then
					    TextBox.SelectedText = e.KeyChar.ToUpper(e.KeyChar)
					    e.Handled = True
					End If
				End If
			End If
		End If
	End Sub

Private Sub TextBox1_KeyPress (...) Handles TextBox1.KeyPress
  Call KeyPressName(sender, e, False) 'do not allow all characters
End Sub
 
I'd still be interested to know which characters are legal and which aren't. I think you would only need to resort to solutions like these if the list is somewhat specialised. Otherwise, the various shared members of the Char structure should be adequate in most cases. In your example JB, you could just use:
VB.NET:
e.Handled = (Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsLetter(e.KeyChar) AndAlso e.Keychar <> "'"c AndAlso e.KeyChar <> "-"c)
 
Back
Top