Results 1 to 4 of 4

Thread: [RESOLVED] check for certain characters in a string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    Resolved [RESOLVED] check for certain characters in a string

    How can i check, in a string, so that the only a string can contain, "." and ":", and "a-z/A-Z", and "0-9" are allowed, and the strings with other characters such as !@#$%^&*(){};'"/?<>|¢£¤¥ and so on are ignored?
    ......

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: check for certain characters in a string

    there might be better ways,

    VB Code:
    1. Private Function IsStringOk(strString As String) As Boolean
    2.  
    3. Dim i     As Integer
    4. Dim strS  As String
    5.  
    6.     For i = 48 To 57
    7.         strS = strS & Chr$(i)
    8.     Next i
    9.     For i = 65 To 90
    10.         strS = strS & Chr$(i)
    11.     Next i
    12.     For i = 97 To 122
    13.         strS = strS & Chr$(i)
    14.     Next i
    15.    
    16.    
    17.    Debug.Print strS
    18.    
    19.     For i = 1 To Len(strString)
    20.      
    21.         If Not InStr(1, strS, Mid$(strString, i, 1)) > 0 Then
    22.             IsStringOk = False
    23.             Exit Function
    24.         End If
    25.        
    26.     Next i
    27.    
    28.     IsStringOk = True
    29.  
    30. End Function
    31.  
    32. Private Sub Command1_Click()
    33. If Not IsStringOk("abc?") Then MsgBox "notOk"
    34. End Sub

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: check for certain characters in a string

    VB Code:
    1. Public Function RemoveInvalidChars(ByVal InputStr As String) As String
    2. Dim i As Integer
    3. dim sTemp As String
    4.    
    5.    sTemp = InputStr
    6.    For i = 0 to 255 'or greater, i based on 255 ascii
    7.       Select Case i
    8.       Case 48 To 57, 65 To 90, 97 To 122
    9.          'skip
    10.       Case Else  'replace these characters with ""
    11.          sTemp = Replace(sTemp, Chr(i), "", vbBinaryCompare)
    12.       End Select
    13.    Next
    14.    RemoveInvalidChars = sTemp
    15. End Function

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    Re: check for certain characters in a string

    thanks
    ......

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