Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy question: Instr : how to determine if it's AlphaNumeric?

Status
Not open for further replies.

eramgarden

Programmer
Aug 27, 2003
279
US
I have 3 string:

ABCD,
1000
UA89 OR A80C ( any combination of number and letter)


I want to determine if the string passed is Alpha Numeric..

====
this is what I have:

Const cnstNumber = "0123456789"
Const cnstAlpha = "ABCDEFGHIJKLMNOPQRSTXYZV"

If (InStr(cnstAlpha, Left(strsitecode, 1)) = 0 And InStr(cnstNumber, Right(strsitecode, 1)) > 0) Then
<..code here ...>
end if

====
That works for ALL numbers, for ALL letters BUT NOT for AlphaNumeric...

any ideas? thanks
 
Try this.

I assume the missing letters in your constant was a typo, and you are only looking for capital letters.

Dim I As Integer
Dim S As String
Const cnstNumber = &quot;0123456789&quot;
Const cnstAlpha = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;

Sub IsAlphaNumeric

For I = 1 To Len(Text1.Text)
S = Mid$(strsitecode, I, 1)
If (InStr(cnstAlpha, S) = False) And (InStr(cnstNumber, S) = False) Then
MsgBox &quot;NOT Alphanumeric&quot;
Exit Sub
End If
Next I

MsgBox &quot;Alphanumeric&quot;

End Sub


Robert
 
I tried it but doesnt seem to work:


Const cnstNumber = &quot;0123456789&quot;
Const cnstAlpha = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;

For i = 1 To Len(strsitecode)
If ((InStr(cnstNumber, Mid$(strsitecode, i, 1)) = 0) And (InStr(cnstAlpha, Mid$(strsitecode, i, 1) = 0))) Then
MsgBox (&quot;not alpha&quot;)
Exit For
End If
Next i

===
??? am i missing something??

my strsitecode is 1000 but it doenst go down the &quot;msg box&quot;.
I even tried using &quot;false&quot; , but same result.
 
The code you posted only tests the first character in the string is not alpha and the last character is alpha. You want to loop through the string and test each character for alpha and numeric. If, after looping through the string you have found both to be true in one or more instances then your string is alphanumeric. The code below illustrates this. It also looks for characters not in your two constant lists. hope this helps.
[rainbow]

Const cnstNumber = &quot;0123456789&quot;
Const cnstAlpha = &quot;ABCDEFGHIJKLMNOPQRSTUXYZVabcdefghijklmnopqrstuxyzv&quot;
Dim strsitecode As String
Dim x As Integer
Dim boolIsNum As Boolean
Dim boolIsAlpha As Boolean
Dim boolSpec As Boolean
strsitecode = InputBox(&quot;Enter Test String here&quot;, &quot;AlphaNumeric Testarooni&quot;)
boolIsAlpha = False
boolIsNum = False
boolSpec = False
'Loop through strsitecode and evaluate one character at a time
For x = 1 To Len(strsitecode)
If InStr(cnstNumber, Mid(strsitecode, x, 1)) > 0 Then
'character is numeric
boolIsNum = True
ElseIf InStr(cnstAlpha, Mid(strsitecode, x, 1)) > 0 Then
'character is alpha
boolIsAlpha = True
Else
'character is neither
boolSpec = True
End If
Next x
'test values of the three booleans to determine string contents!
If boolIsAlpha = True And boolIsNum = True And boolSpec = False Then
MsgBox &quot;AlphaNumeric with no special characters&quot;
ElseIf boolIsAlpha = True And boolnum = False And boolSpec = False Then
MsgBox &quot;Alpha with no special characters&quot;
ElseIf boolIsNum = True And boolIsAlpha = False And boolSpec = False Then
MsgBox &quot;numeric with no special characters&quot;
ElseIf boolSpec = True Then
MsgBox &quot; Special Characters detected&quot;
End If


Sam
 
I had added the omitted &quot;U&quot; but after posting I noticed &quot;W&quot; was missing form the Alpha Const. I also added lower case as well.

Sam
 
eramgarden,

Try it just as I listed the code. You are trying to use only part of the code I posted. I assure you it works.

Robert
 
Maybe I'm reading this wrong, but isn't something like this easier?

Private Sub Form_Load()

Dim strTest As String

strTest = &quot;A32422d&quot;


If IsNumeric(strTest) = True Then
MsgBox strTest & &quot; is a number&quot;
Else
MsgBox strTest & &quot; is alphanumic&quot;
End If


End Sub


Be an idiot:
 
Option Explicit

Private Sub Form_Load()
Dim strTest As String
strTest = &quot;A32422d&quot;
Select Case True
Case IsNumeric(strTest)
MsgBox strTest & &quot; is a number&quot;
Case IsDate(strTest)
MsgBox strTest & &quot; is a date&quot;
Case IsNull(strTest)
MsgBox strTest & &quot; is a NULL&quot;
Case Else
MsgBox strTest & &quot; is a Alpanumeric&quot;
End Select
End Sub



peterguhl@yahoo.de
 
Excause ponerse I've crossed because essentally we make the same thing.

peterguhl@yahoo.de
 
Drop this into a public module:
Code:
Option Explicit

Private Declare Function IsCharAlphaNumeric Lib &quot;user32&quot; Alias &quot;IsCharAlphaNumericA&quot; (ByVal cChar As Byte) As Long

Public Function IsStringAlphaNumeric(sString As String) As Boolean
    Dim iLength As Integer
    Dim x       As Integer
    Dim lChar   As Byte
    
    iLength = Len(sString)
    
    For x = 1 To iLength
        lChar = Asc(Mid$(sString, x, 1))
        If IsCharAlphaNumeric(lChar) = 0 Then
            IsStringAlphaNumeric = False
            Exit For
        End If
        IsStringAlphaNumeric = True
    Next
End Function

Call it like this:

Dim As Boolean

IsAlphaNumeric = IsStringAlphaNumeric(&quot;SomeString123&quot;)
Debug.Print IsAlphaNumeric
IsAlphaNumeric = IsStringAlphaNumeric(&quot;Some String,123&quot;)
Debug.Print IsAlphaNumeric

 
I understood, possibly in error, that he wanted to differentiate between the three examples he provided.
ABCD (all letters)
1000 (all numbers)
UA89 OR A80C ( any combination of number and letter)

That's what the code example I posted was intended for. I guess we need clarification from the original poster.




Sam
 

>I want to determine if the string passed is Alpha Numeric..
Code:
AlphaNumeric is &quot;12134421aifjaksgj&quot;
Numeric      is &quot;1213442&quot;
Alpha        is &quot;aifjaksgj&quot;

This isn't Alpha or Numeric:
             &quot;  , ; # * +) &quot;
 
CCLINT,
I agree with
AlphaNumeric is &quot;12134421aifjaksgj&quot;
Numeric is &quot;1213442&quot;
Alpha is &quot;aifjaksgj&quot;

This isn't Alpha or Numeric:
&quot; , ; # * +) &quot;

but I ASSUMED (yeah, I know, dumb thing to do) that the original poster didn't understand. When will I learn? X-)


Sam
 

With-out testing it, TheVampire's code should also work, except you need to either first run UCase$() as in
S = UCase$(Mid$(strsitecode, I, 1))
or change the compare method of InStr() for the Alpha test to Text and not Binary.
(InStr(cnstAlpha, S, vbTextCompare)) = False)

(and either use a string variable &quot;strsitecode&quot; or the text1.Text)

sdraper, guess what? Next time you will do it the other way, and it will be also &quot;wrong&quot;, and this time I still may be &quot;wrong&quot;, and am just assuming that the leading question means what I interpet it as being correct[lol]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top