Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TuToRiAl] Restricting TextBox Input (Numeric Only TextBox, etc...)
#1
Hello guys, today I will be showing you a tutorial on how to restrict textbox input, which is very useful for many things such as numeric only textboxes, textboxes for your email with the @ sign, much much more. You may not know now, but when you need it for future reference your going to be saying "Thank L3g1tWa5te!" in your head... LOL... Now for the code and procedure.

Steps:

1) Enter the following code on the top of the form.

Code:
Public Class Form1
    Dim charactersDisallowed As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,<>./?; :'{}[]=+-_)(*&^%$#@!~`|\uç▲ìα▲╜KÄ╤ZlÆn╘"

2) Now for TextBox1_TextChanged event code:

Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim theText As String = TextBox1.Text
        Dim Letter As String

        For x As Integer = 0 To TextBox1.Text.Length - 1
            Letter = TextBox1.Text.Substring(x, 1)
            If charactersDisallowed.Contains(Letter) Then
                theText = theText.Replace(Letter, String.Empty)
            End If
        Next
        TextBox1.Text = theText
        TextBox1.Select(TextBox1.Text.Length, 0)
    End Sub

Your done. Now your textbox allows every kind of character thats not in the string "charactersDisallowed", as you can tell numbers are the only type of input allowed.


Now to make it so the textbox can only have certain characters, so the only characters that are allowed are in the string.

1) Now the same thing as before, enter the following code on the top/beginning of the form.

Code:
Public Class Form1

    Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890"

2) Now for TextBox2_TextChanged event code:

Code:
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Dim CurrentText As String = TextBox2.Text
        Dim Input As String
        Dim SelectionIndex As Integer = TextBox2.SelectionStart
        Dim Adjust As Integer

        For x As Integer = 0 To TextBox2.Text.Length - 1
            Input = TextBox2.Text.Substring(x, 1)
            If charactersAllowed.Contains(Input) = False Then
                CurrentText = CurrentText.Replace(Input, String.Empty)
                Adjust = 1
            End If
        Next
        TextBox2.Text = CurrentText
        TextBox2.Select(SelectionIndex - Adjust, 0)
    End Sub

Thats it. Now the TextBox only allows the characters in the string, everything else is blocked for the text boxes' input.
[Image: t5BWm.png]
Reply
#2
From a quick glance this tutorial looks great. Good quality share Smile

Another thing to note which is to do with textbox characters is that you can set a text box to an option of hiding the characters if you want a login page or just something where you want to hide the input as you type it in. The option can be found in the property list for the textbox itself.
Reply
#3
(10-03-2010, 09:59 PM)Infinity Wrote: From a quick glance this tutorial looks great. Good quality share Smile

Another thing to note which is to do with textbox characters is that you can set a text box to an option of hiding the characters if you want a login page or just something where you want to hide the input as you type it in. The option can be found in the property list for the textbox itself.

Ahhh yes! That would be set into the PasswordChar = "*" or that bullet point thing character.
[Image: t5BWm.png]
Reply
#4
Good tutorial. Thanks very much for sharing.
Reply
#5
Thanks a lot what a awesome tut :O, keep up good work man.
[Image: 15nrtb7.jpg]
Reply
#6
Nice tutorial, thanks for sharing.
Reply
#7
Not trying to discredit you, Im just sharing some knowledge. A better way to do something like this (instead of looping through the text each time) would be this.

not allowed
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim NotAllowed As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,<>./?; :'{}[]=+-_)(*&^%$#@!~`|\"
        If NotAllowed.IndexOf(e.KeyChar) <> -1 Then
            e.KeyChar = Nothing
        End If
End Sub

allowed
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim Allowed As String = "abcdefghijklmnopqrstuvwxyz@"
        If Allowed.IndexOf(e.KeyChar) = -1 Then
            e.KeyChar = Nothing
        End If
End Sub
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to display google captcha picture on form and add to textbox hit ok to bypass polas 0 1,266 09-14-2012, 01:59 PM
Last Post: polas
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,535 08-30-2012, 10:14 PM
Last Post: Resistance
  [Release] TextBox Color Changer [Release] Resistance 23 6,979 10-14-2011, 02:55 PM
Last Post: Resistance
  {TuToRiAl} How To Verify If Your App. Was Ran As Administrator {TuToRiAl} Resistance 11 6,017 10-11-2011, 04:30 PM
Last Post: Genuine
  How to set a textbox's value [VB] James Ford 10 2,207 09-01-2011, 02:15 AM
Last Post: James Ford

Forum Jump:


Users browsing this thread: 7 Guest(s)