none
Using If Staments for accepting Alphanumeric data in Textboxes only RRS feed

  • Question

  • Good Afternoon Sir and Madam

    Can You help me find an If statement command that only accepts Alphanumeric data in textboxes In Visual Basic 2015 Express?  Thank You.

    Sincerely Yours,

    James Delorn Howland Jr.

    Monday, November 28, 2016 7:58 PM

Answers

  • Can You help me find an If statement command that only accepts Alphanumeric data in textboxes In Visual Basic 2015 Express?

    The usual procedure would be to iterate over the characters in the string and test for a numeric range of ASCII values.  That may fail for certain encodings.

    For Each c As Char In TextBox1.Text
        If AscW(c) < 48 OrElse (AscW(c) > 57 AndAlso AscW(c) < 65) OrElse (AscW(c) > 90 AndAlso AscW(c) < 97) OrElse AscW(c) > 122 Then
            MsgBox("Invalid")
            Exit For
        End If
    Next
    

    Monday, November 28, 2016 8:22 PM
  • For each c as Char in TextBox1.Text
       If Char.IsLetterOrDigit(c) then ........


    Lloyd Sheen

    Monday, November 28, 2016 8:52 PM
  • You can also use a Regular Expressions filter:

            Dim AlphaNumText As New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9]*$")
            If Not AlphaNumText.IsMatch(AlphaNumTextBox.Text) Then
                MsgBox("Invalid alphanumeric text")
            End If


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, November 28, 2016 9:29 PM
  • Hello,

    Another idea is to encapsulate the logic into a custom TextBox e.g.

    Imports System.Text.RegularExpressions
    Public Class AlphaNumericTextBox
        Inherits TextBox
        Protected Overrides Sub OnTextChanged(e As EventArgs)
            MyBase.OnTextChanged(e)
            Dim charsFromEnd = Text.Length - SelectionStart
            Text = Regex.Replace(Text, "\W", "", RegexOptions.None)
            SelectionStart = Text.Length - charsFromEnd
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 28, 2016 11:29 PM
    Moderator
  • Good Afternoon Sir and Madam

    Can You help me find an If statement command that only accepts Alphanumeric data in textboxes In Visual Basic 2015 Express?  Thank You.

    Sincerely Yours,

    James Delorn Howland Jr.

    This code uses some If logic to only allow Alphanumeric data to be entered into a TextBox. Pasting can not be used with the TextBox.

    On the other hand Visual Studio 2015 Community edition is free and provides more capability than Express editions although I can't find a Visual Studio 2015 Express edition or Visual Basic 2015 Express edition.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Char.IsPunctuation(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Then 'Allows only Alphanumeric and some keys like delete, backspace, enter, etc in TextBox1
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.Control AndAlso e.KeyCode = Keys.V Then e.SuppressKeyPress = True 'Disallows paste into TextBox1
        End Sub
    
    End Class


    La vida loca

    Tuesday, November 29, 2016 4:16 AM
  • Hi James,

    Thank you for posting in MSDN Forum.

    If you assign true to the value of the property Handle of the event arguments, the cancellation of the event will cause unwanted to be ignored. Please check and refer the following code snippet:

    Private Sub TextBox1_KeyPress(sender As Object, e As 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
    
        End Sub

    For more information you could refer that:

    https://msdn.microsoft.com/en-us/library/system.char.isletter.aspx

    http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 29, 2016 7:51 AM
    Moderator

All replies

  • Can You help me find an If statement command that only accepts Alphanumeric data in textboxes In Visual Basic 2015 Express?

    The usual procedure would be to iterate over the characters in the string and test for a numeric range of ASCII values.  That may fail for certain encodings.

    For Each c As Char In TextBox1.Text
        If AscW(c) < 48 OrElse (AscW(c) > 57 AndAlso AscW(c) < 65) OrElse (AscW(c) > 90 AndAlso AscW(c) < 97) OrElse AscW(c) > 122 Then
            MsgBox("Invalid")
            Exit For
        End If
    Next
    

    Monday, November 28, 2016 8:22 PM
  • For each c as Char in TextBox1.Text
       If Char.IsLetterOrDigit(c) then ........


    Lloyd Sheen

    Monday, November 28, 2016 8:52 PM
  • You can also use a Regular Expressions filter:

            Dim AlphaNumText As New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9]*$")
            If Not AlphaNumText.IsMatch(AlphaNumTextBox.Text) Then
                MsgBox("Invalid alphanumeric text")
            End If


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, November 28, 2016 9:29 PM
  • Hello,

    Another idea is to encapsulate the logic into a custom TextBox e.g.

    Imports System.Text.RegularExpressions
    Public Class AlphaNumericTextBox
        Inherits TextBox
        Protected Overrides Sub OnTextChanged(e As EventArgs)
            MyBase.OnTextChanged(e)
            Dim charsFromEnd = Text.Length - SelectionStart
            Text = Regex.Replace(Text, "\W", "", RegexOptions.None)
            SelectionStart = Text.Length - charsFromEnd
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 28, 2016 11:29 PM
    Moderator
  • Good Afternoon Sir and Madam

    Can You help me find an If statement command that only accepts Alphanumeric data in textboxes In Visual Basic 2015 Express?  Thank You.

    Sincerely Yours,

    James Delorn Howland Jr.

    This code uses some If logic to only allow Alphanumeric data to be entered into a TextBox. Pasting can not be used with the TextBox.

    On the other hand Visual Studio 2015 Community edition is free and provides more capability than Express editions although I can't find a Visual Studio 2015 Express edition or Visual Basic 2015 Express edition.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Char.IsPunctuation(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Then 'Allows only Alphanumeric and some keys like delete, backspace, enter, etc in TextBox1
                e.Handled = True
            End If
        End Sub
    
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.Control AndAlso e.KeyCode = Keys.V Then e.SuppressKeyPress = True 'Disallows paste into TextBox1
        End Sub
    
    End Class


    La vida loca

    Tuesday, November 29, 2016 4:16 AM
  • Hi James,

    Thank you for posting in MSDN Forum.

    If you assign true to the value of the property Handle of the event arguments, the cancellation of the event will cause unwanted to be ignored. Please check and refer the following code snippet:

    Private Sub TextBox1_KeyPress(sender As Object, e As 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
    
        End Sub

    For more information you could refer that:

    https://msdn.microsoft.com/en-us/library/system.char.isletter.aspx

    http://msdn.microsoft.com/en-us/library/system.char.isdigit.aspx

    Best Regards,

    Neda Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 29, 2016 7:51 AM
    Moderator