I've seen a post here about this, but how do i filter out all characters except the alphanumeric ones (a,b,c... 0,1,2..)?
Any help would be appreciated.
Printable View
I've seen a post here about this, but how do i filter out all characters except the alphanumeric ones (a,b,c... 0,1,2..)?
Any help would be appreciated.
Add this to the KeyPress event of the TextBox
Code:If Chr$(KeyAscii) Like "#" Or Chr$(KeyAscii) Like "[A-z]" Then KeyAscii = 0
Sorry, it should be:
Code:If Not(Chr$(KeyAscii) Like "#" Or Chr$(KeyAscii) Like "[A-z]") Then KeyAscii = 0
Complete solution which prevents users pasting invalid data, while allowing valid data to be pasted in:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
'Allows alphanumeric data and backspace, no decimal point and no spaces.
If Not ((KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii > 64 And KeyAscii < 91) _
Or (KeyAscii > 96 And KeyAscii < 123) Or (KeyAscii = 8)) Then KeyAscii = 0 'faster than like operator
End Sub
Private Function NuIsAlphaNumeric(ByVal Text As String) As Boolean
'Allows alphanumeric data and backspace, no decimal point and no spaces.
'Can use this function to validate multiple textboxes
Dim ba() As Byte, i&, c As Byte, countdp As Integer, pos As Integer
ba = StrConv(Text, vbFromUnicode)
For i = 0 To UBound(ba)
c = ba(i)
If Not ((c > 47 And c < 58) Or (c > 64 And c < 91) _
Or (c > 96 And c < 123)) Then Exit Function 'faster than like comparisons
Next i
NuIsAlphaNumeric = True
End Function
Private Sub Text1_Validate(Cancel As Boolean)
If Not NuIsAlphaNumeric(Text1) Then Cancel = True
If Cancel = True Then MsgBox "You are only permitted to enter " & _
"alphanumeric data [a-z]:[A-Z]:[0-9]. Please ammend"
End Sub
YO Nucleus -
Dim char as byte
If Not instr('alpha & nums',char) then exit function
Is faster
Yo Ho ho ho ho, oh no Jim string comparisons are slowwww. Numerical comparison is fastest.
You can clean out your validate code event by omitting the 2nd If statement.
Code:Private Sub Text1_Validate(Cancel As Boolean)
If Not NuIsAlphaNumeric(Text1) Then
Cancel = True
MsgBox "You are only permitted to enter " & _
"alphanumeric data [a-z]:[A-Z]:[0-9]. Please ammend"
End Sub
True Megatron :D
Yo Nucleus -
Math compares are decidedly faster than string compares. Wrong. Tad faster, maybe, at least in the code below. Significantly faster, maybe not, especially for this one application.
Here are two sample runs on a 12K text file with characters "A..Z" to force both algoithms to goo thru the whole string. PIII-800 256MB memory, SCSI drives, VB 6.0 sp5
Method 1 is numeric method 2 is text
method 1 begin
Method 1 elapsed time: 9
Method 2 begin
Method 2 elapsed time: 11
method 1 begin
Method 1 elapsed time: 8
Method 2 begin
Method 2 elapsed time: 9
method 1 begin
Method 1 elapsed time: 8
Method 2 begin
Method 2 elapsed time: 11
Method 2 is circa 15-20% slower. Method 1 is NOT blazingly fast, compared to method 2. Give credit to people who write compilers.
You were right nums is is faster but not:
NOT (SLOOOWWWW) = TrueQuote:
Yo Ho ho ho ho, oh no Jim string comparisons are slowwww.
Agreed?
:cool:Code:
Const okChars As String = _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmonpqrstuvwxyz"
Dim txtText As String
Dim arrText(15000) As String * 1
Private Sub Form_Load()
doit
End Sub
Sub doit()
Dim startTime As Long
Dim delta As Long
Dim x As Long
Dim bAnswer As Boolean
Open "c:\mytext.txt" For Input As #1
' file contains 12000 ok chars, last one is a "bad" char a deimal point
txtText = Input(Len("mytext.txt"), 1)
Close #1
Debug.Print "method 1 begin"
startTime = GetTickCount
For x = 1 To 500
bAnswer = NuIsAlphaNumeric(txtText)
Next x
delta = GetTickCount - startTime
Debug.Print "Method 1 elapsed time: "; delta
Debug.Print "Method 2 begin"
startTime = GetTickCount
For x = 1 To 500
bAnswer = NuIsAlphaNumeric1(txtText)
Next x
delta = GetTickCount - startTime
Debug.Print "Method 2 elapsed time: "; delta
End Sub
Private Function NuIsAlphaNumeric(ByVal Text As String) As Boolean
Dim ba() As Byte, i&, c As Byte, countdp As Integer, pos As Integer
NuIsAlphaNumeric = False
ba = StrConv(Text, vbFromUnicode)
For i = 0 To UBound(ba)
c = ba(i)
If Not ((c > 47 And c < 58) Or (c > 64 And c < 91) _
Or (c > 96 And c < 123)) Then Exit Function 'faster than like comparisons
Next i
NuIsAlphaNumeric = True
End Function
Private Function NuIsAlphaNumeric1(ByVal Text As String) As Boolean
Dim i&, j&
NuIsAlphaNumeric1 = False
j = Len(Text)
For i = 1 To j
If InStr(okChars, Mid(Text, i, 1)) = 0 Then Exit Function
Next i
NuIsAlphaNumeric1 = True
End Function