|
-
May 5th, 2004, 07:38 PM
#1
Thread Starter
Hyperactive Member
Validating as Alphanumeric
How can I validate whether a string is alphnumeric?
A cynic knows the price of everything but the value of nothing.
-
May 5th, 2004, 09:00 PM
#2
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
-
May 5th, 2004, 10:07 PM
#3
Fanatic Member
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: )
"Knowledge is gained when different people look at the same information in different ways"
- Louis Pasteur
-
May 6th, 2004, 03:38 AM
#4
don't 4get ascii chrs 97 To 122 for lower case
-
May 6th, 2004, 07:29 AM
#5
Fanatic Member
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?!! )
"Knowledge is gained when different people look at the same information in different ways"
- Louis Pasteur
-
May 6th, 2004, 08:14 AM
#6
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
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
May 6th, 2004, 09:21 AM
#7
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: )
Isnt viable unless you suppress the right click popup -> paste
-
May 17th, 2004, 05:36 AM
#8
note the Ucase - I tested the above and it allows both lower and upper case (sneaky eh?!! )
good point ahara - missed that
-
May 17th, 2004, 06:12 AM
#9
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|