How can I validate whether a string is alphnumeric?
Printable View
How can I validate whether a string is alphnumeric?
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
call it like this to make it do what u want: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
where StringToVarify is your stringCode:VarifyOrStripInvalidChrs(StringToVarify,True ,True )
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:
Private Sub Text7_KeyPress(KeyAscii As Integer) If Asc(UCase(Chr(KeyAscii))) >= 65 And Asc(UCase(Chr(KeyAscii))) <= 90 Then KeyAscii = KeyAscii ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then KeyAscii = KeyAscii ElseIf KeyAscii = 8 Then 'allow backspace key KeyAscii = KeyAscii Else KeyAscii = 0 End If End Sub
(great and easy way to prevent at the source incorrect entry of data - guaranteed!!!!afrog: )
don't 4get ascii chrs 97 To 122 for lower case ;)
VB Code:
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?!!:bigyello: )
Yet another way...
VB Code:
Private Declare Function IsCharAlphaNumeric Lib "user32" Alias "IsCharAlphaNumericA" (ByVal cChar As Byte) As Long Private Sub Text1_KeyPress(KeyAscii As Integer) If Not CBool(IsCharAlphaNumeric(KeyAscii)) Then Select Case KeyAscii Case vbKeyBack, vbKeyDelete Case Else KeyAscii = 0 End Select End If End Sub
Isnt viable unless you suppress the right click popup -> pasteQuote:
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:
Private Sub Text7_KeyPress(KeyAscii As Integer) If Asc(UCase(Chr(KeyAscii))) >= 65 And Asc(UCase(Chr(KeyAscii))) <= 90 Then KeyAscii = KeyAscii ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then KeyAscii = KeyAscii ElseIf KeyAscii = 8 Then 'allow backspace key KeyAscii = KeyAscii Else KeyAscii = 0 End If End Sub
(great and easy way to prevent at the source incorrect entry of data - guaranteed!!!!afrog: )
good point ahara - missed thatQuote:
note the Ucase - I tested the above and it allows both lower and upper case (sneaky eh?!! )
Here is another quick simple function.VB Code:
Private Sub Command1_Click() MsgBox IsAlphaNumeric("e") MsgBox IsAlphaNumeric("#") MsgBox IsAlphaNumeric("5") MsgBox IsAlphaNumeric("+") End Sub Private Function IsAlphaNumeric(ByVal strChar As String) As Boolean Dim strAlphaNumeric As String strAlphaNumeric = "1234567890" & _ "abcdefghijklmnopqrstuvwxyz" & _ "ABCDEFGHIJKLMONPQRSTUVWXYZ" IsAlphaNumeric = InStr(strAlphaNumeric, strChar) End Function