Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alphanumeric textbox

Status
Not open for further replies.

Kingkumar

Programmer
Jan 21, 2003
167
US
hi,
i know it might be obvious for someppl but i am struggling with this one.
I need to allow only alphanumeric values in a textbox( the user should be able to enter only alphabets and numbers) and no special characters or -ve numbers )
how can i achieve this in vb.net for winforms (mine is a windows application)
thanks alot for ur help
regards,
king
 
This is what I did,

Code:
    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

Here Dim cInvalidChars() As Char contains a list of all invalid characters. You can add more to it or can remove from it.

-Kris
 
In the Key Pressed event of your textbox, obtain the ascii code value of the key, and compare it against the ranges you want to accept or decline.

The net result will be that invalid keystrokes will not appear in your textbox.
 
In KeyPress Event
Select Case e.KeyChar
Case "A"c to "Z"c
Case "a"c to "z"c
Case "0"c to "9"c
Case " "c
Case Else
' Allow Tab, Enter , Cntrl+C etc
if Convert.ToByte( e.KeyChar) > 32 then
e.Handled = True ' Disallow
End If
End Select



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
And nobody suggests regex, so I will.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
A keypress event-handler is another possibility. You could find this and other related code at:
Look under vb.net.

' Forcing Alphanumeric data entry on textbox input
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim ch As Char = e.KeyChar
If Not ch.IsLetter(ch) And Not ch.IsNumber(ch) Then
' Kill the entered character
e.Handled = True
End If
 
Thank you John, I didn't think of this very practical aspect.
The revised code would be as follows, with allowance for backspace only. Enter and tab could be added similarly.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' store character typed
Dim ch As Char = e.KeyChar
If ch.IsLetter(ch) Or ch.IsNumber(ch) Or Convert.ToByte(ch) = 8 Then
' OK for alphanumeric and Backspace
Else
' Kill the entered character
e.Handled = True
End If
End Sub
 
Thanks a lot guys.
i appreciate your help.
-king
 
John,

Thank you for this one too!

I was very careful with these details when I was writing Fortran code, taking constants out of the loop, unfolding loops, and so on, as any or every CPU cycle saved could make a difference when computers were expensive and occupied literally rooms.

Lately, I have been too relaxed, and overlooked these newer features that cost nothing to code, but give substantial gains for repetitive code like keypresses.

Happy Programming.
 
There is insignificant gain in performance for keypresses but by doing it always, you will not forget to do it when it counts. The AND and ELSE were originally "scheduled" to be short circuited but the "old line" VB people won out so we are stuck with AndAlso and OrElse.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Personally, I would have been with the old school where short-circuiting was called unsafe optimisation. In addition, there would be less trouble for the compiler manufacturer if the programmer is to take full responsibility of the risks of short-circuiting.

I thought of having the compiler sort out the risks, but then I realise that there are many possibilities: module variables in functions, static variables, input/output, etc, and the compiler will end up acting very conservatively. Leaving the ultimate decision to the programmer may not be such a bad idea after all.

Happy Programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top