Solved

IsAlphabet(string) ??

Posted on 2000-03-02
19
1,406 Views
Last Modified: 2008-03-04
In VB6 Enterprice, we can used IsNumeric(string) to check string with all numeric.
Is there any built-in function to check a string with all alphabet?
or please suggest a best solution.

TQ;
0
Comment
Question by:pkimk
  • 7
  • 4
  • 4
  • +3
19 Comments
 
LVL 1

Expert Comment

by:wolfjjj
ID: 2579271
heres a quick piece of ungly code that will only allow characters a to z and A to Z

Public Function IsAlpha(inString As String) As Boolean
  strlen = Len(inString)
  alpha = True
 
  For i = 1 To strlen
    thechar = Mid(inString, i, i)
    If (Asc(thechar) <= Asc("a") Or Asc(thechar) >= Asc("z")) And _
       (Asc(thechar) <= Asc("A") Or Asc(thechar) >= Asc("Z")) Then
      alpha = False
    End If
  Next i
 
  IsAlpha = alpha
End Function


havent tested it very much *shrug* might be what u want
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 20 total points
ID: 2579308
Oh wow.. 20 points!!!.. LOL. Nevertheless.. No.. there is no alpha test.. <sigh>.. But the function below should allow you to test for any characters in a string that you like.. <smile>.

<---------- Code ---------->

Public Function xIsAlpha _
(Byval strInput as String) _
as Boolean

Const strTEST = "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]" ' Or any other characters you like

xIsAlpha = True
Dim intIndex As Integer
For intIndex = 1 To Len(strInput)
'   Ucase used to test for both upper and lower case
    If Not UCase(Mid(strInput, intIndex, 1)) Like strTEST _
    Then
        xIsAlpha = False
        Exit For
    End If
Next intIndex

End Function

0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2579345
Wolfjjj - according to the rules of EE you shouldnt lock a question unless you're absolutely sure it is the right answer....

However I reckon you're pretty much right - but could be simplified slightly using lcase$ so you only need to test for lower case letters :

Public Function IsAlpha(byval inString As String) As Boolean
  strlen = Len(inString)
  alpha = True
   
  inString = lcase$(instring)

  For i = 1 To strlen
    thechar = Mid(inString, i, i)
    If (Asc(thechar) <= Asc("a") Or Asc(thechar) >= Asc("z")) Then
      alpha = False
    End If
  Next i
   
  IsAlpha = alpha
End Function


0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:paulstamp
ID: 2579360
Just noticed... Wolfjjj - your code wont actually work - the 2nd param in mid$ should be a "1", not "i"

Public Function IsAlpha(byval inString As String) As Boolean
   
  inString = lcase$(instring)

  For i = 1 To Len(inString)
    thechar = Mid$(inString, i, 1)
    If thechar <= "a" Or thechar >= "z" Then
        isalpha = False
        Exit For
    Endif
 
  Next i
   
End Function




0
 
LVL 14

Expert Comment

by:wsh2
ID: 2579379
Psssst.. Paulstamp.. don't forget to add a IsAlpha = True before your loop.. <wink>. Also.. move your Lcase down to your actual test (which should NOT have the = sign in it). Here.. try this out.. <smile>

<----- Code ----->

Public Function IsAlpha(byval inString As String) As Boolean
     
  isAlpha = True
  For i = 1 To Len(inString)
    If Lcase(Mid$(inString, i, 1))      < "a" _
    Or Lcase(Mid$(inString, i, 1)) > "z" _
    Then
       isalpha = False
       Exit For
    End If
  Next i
     
End Function
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2579398
<laughing>.. Yikes look at me.. I forgot to declare the index... LOL. Please add another line to the beginning of the function.

Dim i as Integer

<sheesh>
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2579411
Oops.. quite right.. missed out the Setting = True at the top, the "<=" I copied and pasted from Wolfjjj's code (my original comment was a modification of his code to make it work more efficiently)

Why move the lcase$ to the actual test - that means performing the conversion every time around the loop instead of just once ?
0
 
LVL 18

Expert Comment

by:deighton
ID: 2579483
Here's my entry.  It has an optional second parameter to allow the space character as valid.

Public Function is_alphabet(ByRef sTest As String, Optional vAllowSpaces) As Boolean

    Dim c As Long
    Dim sChar As String * 1
    Dim bSpaces As Boolean
   
    If IsMissing(vAllowSpaces) Then
        bSpaces = False
    Else
        bSpaces = CBool(vAllowSpaces)
    End If
   
    For c = 1 To Len(sTest)
        sChar = UCase(Mid(sTest, c, 1))
        If (sChar < "A" Or sChar > "Z") And _
        (sChar <> Chr(32) Or bSpaces = False) Then
           
            is_alphabet = False
            Exit Function
           
        End If
    Next
    is_alphabet = True
       
       

End Function


MsgBox is_alphabet("Fred Bloggs", True)
MsgBox is_alphabet("Fred Bloggs", False)
MsgBox is_alphabet("FredBloggs")
MsgBox is_alphabet("Fred Bloggs")
MsgBox is_alphabet("Fred Bloggs55")
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2579490
I did it for readibility.. more than anything else.. and to keep the code simple and short. The Like method I proposed in my submittal is consistant with this. In one line (const strTEST), it shows all the characters being tested for.. any character the developer desires.. without having to do a bunch of asc/char conversions.

With long strings, you are correct in assuming that precasing the string before the tests would provide a performance benefit. But frankly, with 650mhz screamers now being offered by Gateway for less than $1000, I doubt that in the future, anyone will notice the difference in readable/maintainable code versus code that is terribly tight.

All in all, its a tradeoff.. performance vs readability/maintainability.. and really just up to the developers choice.

Thank YOU for asking.. <smile>
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2579500
Hello Deighton.. <smiling>. To add spaces (or anything else in my Like example).. just insert the character in the strTEST constant anywhere between the brackets. Eg...

const strTEST = "[ABCDEFG /1234*^HIJKLMNOPQRSTUVWXYZ]"

and only those characters will be tested as being valid.
0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2579502
wsh2... agreed - I think your answer is a neater solution than mine in as much is it is extendible to allow extra chars without messing around with additional if statements.

I take your point about the performance of newer machines but I cant help thinking that is Microsoft's approach to performance <smile>
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2579565
Its a chicken and egg argument.. Microsoft would never have gotten where it did without Intel.. and likewise. Intel would have been nothing without Microsoft. We both should be happy that today, we don't have to squeeze our code into a 16kb 8088 machine that has two flat tires.. <smile>.

Think for a moment.. the hot machine of 10 years ago.. was a 16mhz 80286.. now, how much software do you run today from that era? You can bet your bottom dollar.. that it was written tight.. but today.. who cares?.. <wink>.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2580046
You could always do it like this:

Function IsAlpha(Source As String) As Boolean
    Dim iVal As Long
    IsAlpha = True
    For iVal = 1 To Len(Source)
        Select Case Asc(UCase(Mid$(Source, iVal, 1)))
            Case 32, 65 To 90: Case Else
                IsAlpha = False
                Exit For
        End Select
    Next iVal
End Function


This will allow A-Z, a-z and space....
If you want any other characters to be allowed, put them in the 1st case statement...


Cheers!
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2580132
Very nice mcrider.. <smile>
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2580159
wsh2, Thanks!  I tend to think in cases instead of ifs... ;-)
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2580171
Too bad the question is already locked... but pkimk is a seasoned user...


Cheers!
0
 

Author Comment

by:pkimk
ID: 2590142
Huh guys, thanks all.

I like wsh2's solution. It's simple, short, readability and maintainability.

I learn more from mcrider and paulstamp's solution.
mcrider's solution is good!
I'll ask for help to credit 10 points each to two of you.

So sad to comment deighton's solution like drawing "snake with leg".
Anyhow, deighton, thank you very much for your posting.
Thank you wolfjjj.

cheers;
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2590277
If you want to credit 10 points to both mcrider and paulstamp, just open 2 new questions in this category with the titles "FOR MCRIDER ONLY" and "FOR PAULSTAMP ONLY", and assign the points...


Cheers!®©
0
 
LVL 18

Expert Comment

by:deighton
ID: 2590728
Thanks a bundle, I won't bother with you in future :O(
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question