Results 1 to 9 of 9

Thread: is letter or digit - textbox characters..

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    is letter or digit - textbox characters..

    how can i tell if a user entered in ONLY a letter a-z and or a digit...

    ive tried..

    Dim mytext As Char
    If mytext.IsLetterOrDigit(txtDistrictDB.Text) = True Then
    MsgBox("yes")
    Else
    MsgBox("no")
    End If

    but if i type in a character such as !@#$%^&*()_ it returns true also.....

    it doesn't work if i try to get the characters in a text box, but if i type in one character than it works.. is there a way around it rather than looping through each character?

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Do you want it to be checked one by one as the user enteres the letters? If so check for the ascii of character to be in range of a-Z (if u like to include capitals too) and also 0-9. But you may also like to trap Space, Back Space, delete...
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    asci range

    do you know what that asci range will be?

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    a-z 97-122
    A-Z is 65-90
    0-9 is 48-57
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could use RegularExpressions:
    VB Code:
    1. 'search for anything other than a-zA-z0-9
    2.         If DirectCast(Regex.Matches(TextBox1.Text, "\W"), MatchCollection).Count > 0 Then
    3.             MsgBox("Contains non alphanumeric chars")
    4.         Else
    5.             MsgBox("Contains only alphanumeric chars")
    6.         End If

    You'll need to import the regularexpressions namespace:

    Imports System.Text.RegularExpressions
    Last edited by Edneeis; Jul 15th, 2003 at 01:33 AM.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Originally posted by Edneeis
    You could use RegularExpressions:
    VB Code:
    1. 'search for anything other than a-zA-z0-9
    2.         If DirectCast(Regex.Matches(TextBox1.Text, "\W"), MatchCollection).Count > 0 Then
    3.             MsgBox("Contains non alphanumeric chars")
    4.         Else
    5.             MsgBox("Contains only alphanumeric chars")
    6.         End If

    You'll need to import the regularexpressions namespace:

    Imports System.Text.RegularExpressions
    Wouldn't that cause a performance problem if he want to check it real time-letter by letter- as Regex checks for the entire text each time?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Lunatic3
    Wouldn't that cause a performance problem if he want to check it real time-letter by letter- as Regex checks for the entire text each time?
    I agree .

    Here is how it should be done :
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         Dim KeyAscii As Integer
    3.         KeyAscii = Asc(e.KeyChar)
    4.  
    5.         Select Case KeyAscii
    6.             Case Asc(0) To Asc(9)
    7.                 MessageBox.Show("You entered number : " & e.KeyChar.ToString)
    8.             Case (97) To (122)
    9.                 MessageBox.Show("You entered lowercase letter : " & e.KeyChar.ToString)
    10.             Case (65) To (90)
    11.                 MessageBox.Show("You entered uppercase letter : " & e.KeyChar.ToString)
    12.         End Select
    13.  
    14.     End Sub
    15. End Class

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    RegularExpressions should be pretty fast but if he is checking letter by letter than it might. I must have misread I thought he didn't want to check letter by letter but instead the whole text at once.
    is there a way around it rather than looping through each character?
    But whatever works, its all good.

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    all you need to do is this , to check if it's a letter / number or if it's another character :
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         If e.KeyChar.IsLetterOrDigit(e.KeyChar) Then
    4.             MessageBox.Show("is a letter or number")
    5.         Else
    6.             MessageBox.Show("is a character , not a number or letter!")
    7.         End If
    8.  
    9.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width