Answered by:
Using If Staments for accepting Alphanumeric data in Textboxes only

Question
-
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
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:55 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 PM
-
For each c as Char in TextBox1.Text If Char.IsLetterOrDigit(c) then ........
Lloyd Sheen
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 7:46 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 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)
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:55 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 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
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:56 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 PM
-
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
- Edited by Mr. Monkeyboy Tuesday, November 29, 2016 4:33 AM
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 7:54 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:27 PM
-
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,
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.- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:27 PM
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
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:55 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 PM
-
For each c as Char in TextBox1.Text If Char.IsLetterOrDigit(c) then ........
Lloyd Sheen
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 7:46 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 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)
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:55 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 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
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 6:56 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:26 PM
-
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
- Edited by Mr. Monkeyboy Tuesday, November 29, 2016 4:33 AM
- Proposed as answer by Neda ZhangModerator Tuesday, November 29, 2016 7:54 AM
- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:27 PM
-
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,
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.- Marked as answer by JAMES VISUAL BASIC LOVER 495 Wednesday, June 26, 2019 11:27 PM