Results 1 to 9 of 9

Thread: Validating as Alphanumeric

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279

    Validating as Alphanumeric

    How can I validate whether a string is alphnumeric?
    A cynic knows the price of everything but the value of nothing.

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    i made a function that would do this ages ago .... abit messey but it will do the job and alot of other usefull things aswell

    Code:
    Public Function VarifyOrStripInvalidChrs(xString As String, AtoZ As Boolean, Numeric As Boolean, Optional ChrsToAccept As String = "", Optional ChrsToReject As String = "", Optional StripChrs As Boolean = False)
        Dim NewXString, ChrsToAcceptList, ChrsToRejectList, CurrentChr
        NewXString = xString
        ChrsToAcceptList = ChrsToAccept
        
        If AtoZ = True Then
            For i = 65 To 90
                ChrsToAcceptList = ChrsToAcceptList & Chr(i)
            Next
            For i = 97 To 122
                ChrsToAcceptList = ChrsToAcceptList & Chr(i)
            Next
        End If
        
        If Numeric = True Then
            For i = 48 To 57
                ChrsToAcceptList = ChrsToAcceptList & Chr(i)
            Next
        End If
        
        For i = 0 To 255
            ChrsToRejectList = ChrsToRejectList & Chr(i)
        Next
        For i = 1 To Len(ChrsToAcceptList)
            CurrentChr = Mid(ChrsToAcceptList, i, 1) 'get the current chr we are working with
            ChrsToRejectList = Replace(ChrsToRejectList, CurrentChr, "")
        Next
        ChrsToRejectList = ChrsToRejectList & ChrsToReject
        
        For i = 1 To Len(ChrsToRejectList)
            CurrentChr = Mid(ChrsToRejectList, i, 1) 'get the current chr we are working with
            NewXString = Replace(NewXString, CurrentChr, "")
        Next
        
        If StripChrs = True Then
            VarifyOrStripInvalidChrs = NewXString
        Else
            VarifyOrStripInvalidChrs = xString = NewXString
        End If
    End Function
    call it like this to make it do what u want:

    Code:
    VarifyOrStripInvalidChrs(StringToVarify,True ,True )
    where StringToVarify is your string

  3. #3
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    this may not apply to your needs, but if it is user input you are validating, place the following in the keypress event:

    VB Code:
    1. Private Sub Text7_KeyPress(KeyAscii As Integer)
    2.    
    3.    If Asc(UCase(Chr(KeyAscii))) >= 65 And Asc(UCase(Chr(KeyAscii))) <= 90 Then
    4.         KeyAscii = KeyAscii
    5.     ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then
    6.         KeyAscii = KeyAscii
    7.     ElseIf KeyAscii = 8 Then 'allow backspace key
    8.         KeyAscii = KeyAscii
    9.     Else
    10.         KeyAscii = 0
    11.     End If
    12.    
    13. End Sub

    (great and easy way to prevent at the source incorrect entry of data - guaranteed!!!!afrog: )
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    don't 4get ascii chrs 97 To 122 for lower case

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    VB Code:
    1. If Asc(UCase(Chr(KeyAscii))) >= 65 And Asc(UCase(Chr(KeyAscii))) <= 90 Then

    note the Ucase - I tested the above and it allows both lower and upper case (sneaky eh?!! )
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Yet another way...
    VB Code:
    1. Private Declare Function IsCharAlphaNumeric Lib "user32" Alias "IsCharAlphaNumericA" (ByVal cChar As Byte) As Long
    2.  
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4.  
    5.     If Not CBool(IsCharAlphaNumeric(KeyAscii)) Then
    6.         Select Case KeyAscii
    7.             Case vbKeyBack, vbKeyDelete
    8.             Case Else
    9.                 KeyAscii = 0
    10.         End Select
    11.     End If
    12.  
    13. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Originally posted by ahara
    this may not apply to your needs, but if it is user input you are validating, place the following in the keypress event:

    VB Code:
    1. Private Sub Text7_KeyPress(KeyAscii As Integer)
    2.    
    3.    If Asc(UCase(Chr(KeyAscii))) >= 65 And Asc(UCase(Chr(KeyAscii))) <= 90 Then
    4.         KeyAscii = KeyAscii
    5.     ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then
    6.         KeyAscii = KeyAscii
    7.     ElseIf KeyAscii = 8 Then 'allow backspace key
    8.         KeyAscii = KeyAscii
    9.     Else
    10.         KeyAscii = 0
    11.     End If
    12.    
    13. End Sub

    (great and easy way to prevent at the source incorrect entry of data - guaranteed!!!!afrog: )
    Isnt viable unless you suppress the right click popup -> paste

  8. #8
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    note the Ucase - I tested the above and it allows both lower and upper case (sneaky eh?!! )
    good point ahara - missed that

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Here is another quick simple function.
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox IsAlphaNumeric("e")
    3.     MsgBox IsAlphaNumeric("#")
    4.     MsgBox IsAlphaNumeric("5")
    5.     MsgBox IsAlphaNumeric("+")
    6. End Sub
    7.  
    8. Private Function IsAlphaNumeric(ByVal strChar As String) As Boolean
    9. Dim strAlphaNumeric As String
    10.    
    11.     strAlphaNumeric = "1234567890" & _
    12.     "abcdefghijklmnopqrstuvwxyz" & _
    13.     "ABCDEFGHIJKLMONPQRSTUVWXYZ"
    14.    
    15.     IsAlphaNumeric = InStr(strAlphaNumeric, strChar)
    16. End Function

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