Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, Hi i have a requiment in vb.net in windows application. Textbox allows only alphanumerics and First letter should be a Characters only not a numbers. Doen't allow the special characters ,symbllos , underlines and spaces. If any of the another charactes should enter then raise the validation message.

Please any one help me.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Aug-13 1:31am    
Could you define "alphanumeric" set? With Unicode, this is not so trivial... :-)
—SA

First of all, please see my comment to the question.

You can easily filter out unwanted characters from input handling the event KeyPress or overriding the virtual method OnKeyPress:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeypress.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx[^].

This event has the cancellation mechanism explained in the second article references above; see also: http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx[^].

If you assign true to the value of the property Handled of the event arguments, the cancellation of the event will cause unwanted character to be ignored. Now, about "alphanumeric". You can check the following predicates:
http://msdn.microsoft.com/en-us/library/system.char.isletter.aspx[^],
http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx[^].

I hope you got the idea.

And one important point: don't forget to allow backspace. By some weird historical reason, this hey is considered as a "character", with a code point #8. If you filter it out with other unwanted characters, edit controls (text boxes or anything like that) won't allow you to delete a previous character.

—SA
 
Share this answer
 
VB
' for example i allow "." and "," but if you not need then just remove those condition.
Function Isnumber(ByVal KCode As String) As Boolean
        Isnumber = True
        If Not IsNumeric(KCode) And KCode <> ChrW(Keys.Back) And KCode <> ChrW(Keys.Enter) And KCode <> "."c And KCode <> ","c Then
            Isnumber = False
            MsgBox("Please Enter Numbers only", MsgBoxStyle.OkOnly)
        End If

    End Function


VB
' put this code under textbox keypress event
  Private Sub textbox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
        If Not Isnumber(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub
 
Share this answer
 
v2
VB
' Boolean flag used to determine when a character other than a number is entered.
    Private nonNumberEntered As Boolean = False

    ' Handle the KeyDown event to determine the type of character entered into the control.
    Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
         Handles TextBox1.KeyDown
        ' Initialize the flag to false.
        nonNumberEntered = False

        ' Determine whether the keystroke is a number from the top of the keyboard.
        If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
            ' Determine whether the keystroke is a number from the keypad.
            If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                ' Determine whether the keystroke is a backspace.
                If e.KeyCode <> Keys.Back Then
                    ' A non-numerical keystroke was pressed. 
                    ' Set the flag to true and evaluate in KeyPress event.
                    nonNumberEntered = True
                End If
            End If
        End If
    End Sub 'textBox1_KeyDown


    ' This event occurs after the KeyDown event and can be used 
    ' to prevent characters from entering the control.
    Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles TextBox1.KeyPress
        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
    End Sub 'textBox1_KeyPress
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900