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?
Printable View
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?
there might be better ways,
VB Code:
Private Function IsStringOk(strString As String) As Boolean Dim i As Integer Dim strS As String For i = 48 To 57 strS = strS & Chr$(i) Next i For i = 65 To 90 strS = strS & Chr$(i) Next i For i = 97 To 122 strS = strS & Chr$(i) Next i Debug.Print strS For i = 1 To Len(strString) If Not InStr(1, strS, Mid$(strString, i, 1)) > 0 Then IsStringOk = False Exit Function End If Next i IsStringOk = True End Function Private Sub Command1_Click() If Not IsStringOk("abc?") Then MsgBox "notOk" End Sub
VB Code:
Public Function RemoveInvalidChars(ByVal InputStr As String) As String Dim i As Integer dim sTemp As String sTemp = InputStr For i = 0 to 255 'or greater, i based on 255 ascii Select Case i Case 48 To 57, 65 To 90, 97 To 122 'skip Case Else 'replace these characters with "" sTemp = Replace(sTemp, Chr(i), "", vbBinaryCompare) End Select Next RemoveInvalidChars = sTemp End Function
thanks