• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1441
  • Last Modified:

IsAlphabet(string) ??

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
pkimk
Asked:
pkimk
  • 7
  • 4
  • 4
  • +3
1 Solution
 
wolfjjjCommented:
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
 
wsh2Commented:
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
 
paulstampCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
paulstampCommented:
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
 
wsh2Commented:
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
 
wsh2Commented:
<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
 
paulstampCommented:
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
 
deightonCommented:
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
 
wsh2Commented:
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
 
wsh2Commented:
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
 
paulstampCommented:
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
 
wsh2Commented:
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
 
mcriderCommented:
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
 
wsh2Commented:
Very nice mcrider.. <smile>
0
 
mcriderCommented:
wsh2, Thanks!  I tend to think in cases instead of ifs... ;-)
0
 
mcriderCommented:
Too bad the question is already locked... but pkimk is a seasoned user...


Cheers!
0
 
pkimkAuthor Commented:
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
 
mcriderCommented:
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
 
deightonCommented:
Thanks a bundle, I won't bother with you in future :O(
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now