Hello World!
I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. -
-
Public Enum MyOption
-
Alpha = 1
-
AlphaNumeric = 2
-
Numeric = 3
-
End Enum
-
-
Public Sub SetCharacter(ByVal CharacterOption As MyOption, _
-
ByVal kp As KeyPressEventArgs)
-
Select Case CharacterOption
-
Case MyOptions.Alpha
-
If Not ( _
-
kp.KeyChar Like "[A-Z]" Or _
-
kp.KeyChar Like "[a-z]" Or _
-
kp.KeyChar = vbBack Or _
-
Asc(kp.KeyChar) = 32) Then
-
kp.KeyChar = vbNullChar
-
End If
-
Case MyOptions.AlphaNumeric
-
If Not ( _
-
kp.KeyChar Like "[A-Z]" Or _
-
kp.KeyChar Like "[a-z]" Or _
-
kp.KeyChar Like "[ñÑ]" Or _
-
kp.KeyChar Like "[0-9]" Or _
-
kp.KeyChar = vbBack Or _
-
Asc(kp.KeyChar) = 32) Then
-
kp.KeyChar = vbNullChar
-
End If
-
Case MyOptions.Numeric
-
If Not ( _
-
kp.KeyChar Like "[0-9]" Or _
-
kp.KeyChar = vbBack Or _
-
Asc(kp.KeyChar) = 32) Then
-
kp.KeyChar = vbNullChar
-
End If
-
End Select
-
-
End Sub
Just place the SetCharacter method to the KeyPress event of a textbox, combobox, etc.
Sample 1. A textbox that accepts only numeric characters - Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
-
SetCharacter(MyOption.Numeric, e)
-
End Sub
As you noticed, the SetCharacter method has 2 parameters-CharacterOption and kp.
CharacterOption - Sets the MyOption value whether you want alpha, alphanumeric, or numeric
kp - Gets the current key that you have pressed.
Rey Sean
Mabuhay ang pinoy : )
5 28484
My first thought was... Why? Then I read the end of it and it changed to Oh!.
Nice, work. Personally I don't tend to need to limit things to alpha or alphanumeric, only numeric. In those cases, if that's the only thing I need to limit on keypress, I'll just write the numeric limiting code once and refer all the keypresses to that one bit of code.
This is pretty friggin nifty though.
HI,
Make use of Regular Expressiona.
Thanks!
Your code is a great idea and very useful...however, I thought it might be a bit more extensible in this format, it's also easier to write unit tests for: - Public Enum Options
-
Alpha
-
AlphaNumeric
-
Numeric
-
End Enum
-
-
Function Pattern(ByVal CharOpt As Options)
-
-
Dim s = String.Empty
-
Select Case CharOpt
-
Case Options.Alpha
-
s = "A-Za-z"
-
Case Options.AlphaNumeric
-
s = "\w"
-
Case Options.Numeric
-
s = "\d"
-
End Select
-
-
'All my patterns allow spaces...
-
Return String.Format("[{0}\s]", s)
-
-
End Function
-
-
Public Sub AllowChars(ByVal CharOpt As Options, _
-
ByVal kp As KeyPressEventArgs)
-
-
'Create list of always allowable keys
-
Dim Allow As Keys() = {Keys.Delete, Keys.Back}
-
-
'If not one of the allowable keys we predefined,
-
'then check the characters entered against the
-
'pattern defined for the selected pattern option,
-
'if it doesn't match, then ditch it...
-
If Not Allow.Contains(AscW(kp.KeyChar)) Then
-
If Not Regex.IsMatch(kp.KeyChar, Pattern(CharOpt)) Then
-
kp.KeyChar = ControlChars.NullChar
-
End If
-
End If
-
-
End Sub
-
-
Private Sub KeyPressed(ByVal sender As Object, _
-
ByVal e As KeyPressEventArgs) _
-
Handles tb1.KeyPress
-
-
AllowChars(Options.Numeric, e)
-
-
End Sub
I will take your comments as complements. Anyway, thank you guys for your comments : )
To madankarmukta, I was thinking about RegularExpressions and I know this is more appropriate and more acceptable than mine LOL. I found a lot of tutorials in the net using this and as you can see in the title "Another way on how ...", so I created another way on how to handle this.
Rey Sean
Just write a few lines to the TextChanged event that fires when the text changes.
The following is an example of alphanumeric characters. - Private Sub TextBox1_TextChange(sender As System.Object, e As System.EventArgs)
-
Dim r As New System.Text.RegularExpressions.Regex("[^A-Za-z0-9]")
-
Dim i As Integer
-
i = textbox1.SelectionStart
-
TextBox1.Text = r.Replace(TextBox1.Text, "")
-
TextBox1.SelectionStart = i
-
End Sub
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sanka |
last post by:
I want to restrict users entering non numeric characters
in a text box
How can I do this?
If there is any code sample please answer or email I'll be
very glad
Thank you
|
by: anonieko |
last post by:
This applies to javascript dynamic textbox onkey
>
>
>
Newsgroups: comp.lang.javascript
From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by
this author
Date: Fri, 15 Jul 2005...
|
by: success_ny |
last post by:
Does anyone have a code snippet to compare those values so I can sort
the array of alpha-numeric values that include both characters and
integers in it?
I.e., if we have values like 4236 and...
|
by: Mark C |
last post by:
All,
Is there such a function that can strip all non alpha ( not between a-z)
characters from a string? I have a function that I currently use that will
strip one character at a time from a...
|
by: MLH |
last post by:
I have a textbox on a form into which an alpha-numeric string
of data is entered. I wish to force the casual user, who would
sometimes use upper case, sometimes not and sometimes MIX
the case -...
|
by: Alpha |
last post by:
Hi, I have a window based applicaton. I want to make sure that the users
enter integer numbers before I save it to the database. I know to code this
in the textbox_validating event but I don't...
|
by: Itar |
last post by:
I'm having a problem supressing characters in a text box. I only want alpha
numeric characters (no special chars). I can handle the TextBox_KeyPress
event to supress the invalid characters when...
|
by: ameen.abdullah |
last post by:
Hi Guys,
I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of...
|
by: .Net Sports |
last post by:
I am checking for text input on a form validation in javascript that
required at least one numeric character along with any number of alpha
characters for a given input text box. The below is a var...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |