Results 1 to 5 of 5

Thread: Alphanumeric

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Alphanumeric

    Hey,

    How can I check whether a character is alphanumeric??

    I know I can check the ASC value and compare it, but are there any other ways?

    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    VB Code:
    1. Public Function IAlphaNum(ByVal pstrChar   As String) As Boolean
    2.    Select Case Asc(pstrCar)
    3.       Case vbKey0 To vbKey9
    4.          IsAlphaNum = True
    5.       Case vbKeyA To vbKeyZ
    6.          IsAlphaNum = True
    7.    End Select
    8. End function

    Woka

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You can combine the two Cases. I also fixed a couple of typos and added the UCase so that it would work for lowercase as well as uppercase letters.

    VB Code:
    1. Public Function IsAlphaNum(ByVal pstrChar As String) As Boolean
    2.    Select Case Asc(UCase(pstrChar))
    3.       Case vbKey0 To vbKey9, vbKeyA To vbKeyZ
    4.          IsAlphaNum = True
    5.    End Select
    6. End Function

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    IF you need to repeat this function a lot you can try something like this which is usually faster then SELECT CASE:

    Code:
    Const  IS_DIG as Byte = 2
    Const  IS_UPP as Byte =4
    Const  IS_LOW as Byte	= 8
    
    Function isalnum(c as Byte) as Byte
        isalnum= ( c + 1) AND ( IS_DIG OR IS_UPP OR IS_LOW) 
    End Function
    -- note I did this from memory, I believe the const declarations are correct. AND OR operations are faster than other compare operations.

  5. #5

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