Connecting Tech Pros Worldwide Help | Site Map

Another way on how to restrict the textbox to accept only alpha or numeric characters

  #1  
Old December 6th, 2008, 10:46 AM
lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 855
Hello World!

I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Enum MyOption
  3.         Alpha = 1
  4.         AlphaNumeric = 2
  5.         Numeric = 3
  6.     End Enum
  7.  
  8.  Public Sub SetCharacter(ByVal CharacterOption As MyOption, _
  9. ByVal kp As KeyPressEventArgs)
  10.         Select Case CharacterOption
  11.             Case MyOptions.Alpha
  12.                 If Not ( _
  13.                     kp.KeyChar Like "[A-Z]" Or _
  14.                     kp.KeyChar Like "[a-z]" Or _
  15.                     kp.KeyChar = vbBack Or _
  16.                     Asc(kp.KeyChar) = 32) Then
  17.                     kp.KeyChar = vbNullChar
  18.                 End If
  19.             Case MyOptions.AlphaNumeric
  20.                 If Not ( _
  21.                         kp.KeyChar Like "[A-Z]" Or _
  22.                         kp.KeyChar Like "[a-z]" Or _
  23.                         kp.KeyChar Like "[ρΡ]" Or _
  24.                         kp.KeyChar Like "[0-9]" Or _
  25.                         kp.KeyChar = vbBack Or _
  26.                         Asc(kp.KeyChar) = 32) Then
  27.                     kp.KeyChar = vbNullChar
  28.                 End If
  29.             Case MyOptions.Numeric
  30.                 If Not ( _
  31.                      kp.KeyChar Like "[0-9]" Or _
  32.                      kp.KeyChar = vbBack Or _
  33.                      Asc(kp.KeyChar) = 32) Then
  34.                     kp.KeyChar = vbNullChar
  35.                 End If
  36.         End Select
  37.  
  38.     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
Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress
  2.         SetCharacter(MyOption.Numeric, e)
  3.     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 : )



  #2  
Old December 8th, 2008, 05:50 PM
Newbie
 
Join Date: Sep 2008
Posts: 10

re: Another way on how to restrict the textbox to accept only alpha or numeric characters


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.
  #3  
Old December 9th, 2008, 09:06 AM
Familiar Sight
 
Join Date: Apr 2008
Posts: 145

re: Another way on how to restrict the textbox to accept only alpha or numeric characters


HI,

Make use of Regular Expressiona.

Thanks!
  #4  
Old December 9th, 2008, 10:00 PM
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757

re: Another way on how to restrict the textbox to accept only alpha or numeric characters


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:
Expand|Select|Wrap|Line Numbers
  1. Public Enum Options
  2.   Alpha
  3.   AlphaNumeric
  4.   Numeric
  5. End Enum
  6.  
  7. Function Pattern(ByVal CharOpt As Options)
  8.  
  9.   Dim s = String.Empty
  10.   Select Case CharOpt
  11.       Case Options.Alpha
  12.           s = "A-Za-z"
  13.       Case Options.AlphaNumeric
  14.           s = "\w"
  15.       Case Options.Numeric
  16.           s = "\d"
  17.   End Select
  18.  
  19.   'All my patterns allow spaces... 
  20.   Return String.Format("[{0}\s]", s)
  21.  
  22. End Function
  23.  
  24. Public Sub AllowChars(ByVal CharOpt As Options, _
  25.                       ByVal kp As KeyPressEventArgs)
  26.  
  27.   'Create list of always allowable keys
  28.   Dim Allow As Keys() = {Keys.Delete, Keys.Back}
  29.  
  30.   'If not one of the allowable keys we predefined, 
  31.   'then check the characters entered against the 
  32.   'pattern defined for the selected pattern option, 
  33.   'if it doesn't match, then ditch it...
  34.   If Not Allow.Contains(AscW(kp.KeyChar)) Then
  35.     If Not Regex.IsMatch(kp.KeyChar, Pattern(CharOpt)) Then
  36.       kp.KeyChar = ControlChars.NullChar
  37.     End If
  38.   End If
  39.  
  40. End Sub
  41.  
  42. Private Sub KeyPressed(ByVal sender As Object, _
  43.                        ByVal e As KeyPressEventArgs) _
  44.   Handles tb1.KeyPress
  45.  
  46.   AllowChars(Options.Numeric, e)
  47.  
  48. End Sub
  #5  
Old December 10th, 2008, 12:31 AM
lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 855

re: Another way on how to restrict the textbox to accept only alpha or numeric characters


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
Reply