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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alphacharacters only

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
How can i check if a string is a letter and not any other character?

instead of doing this:

if strval = "a" or _
strval = "b" or _
strval = "z"


is there another way?
 
Did you look at the Asc() procedure? If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
if it's just one character you could get the asc value and compare it between two ranges....

065 - 090 (uppercase) and 097 - 122 (lowercase)

NB am using qbasic ASC codes :( too lazy to look up to see if there is any difference when dealing with VB - sorry....
 
use the built in isNumeric function to check the string and if the return is true then perform the needed steps A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
here's a generic function that will do what you want

Pass in your string as str
pass in the alphabet as chars


Public Function ContainsOnly(str As String, chars As String) As Boolean
Dim i As Integer
Dim pos As Integer

For pos = 1 To Len(str)
If (InStr(chars, Mid(str, pos, 1)) <= 0) Then
ContainsOnly = False
Exit Function
End If
Next

ContainsOnly = True
End Function
 
You can use the API function: IsCharAlpha.
Drop this in a public module

Option Explicit
Private Declare Function IsCharAlpha Lib &quot;user32&quot; Alias &quot;IsCharAlphaA&quot; (ByVal cChar As Byte) As Long

Public Function IsAlphaA_pFb(ByVal sChar As String) As Boolean
Dim lChar As Long
lChar = Asc(sChar)
IsAlphaA_pFb = IsCharAlpha(lChar)
End Function


Public Function IsAlphaB_pFb(ByVal AsciiChar As Byte) As Boolean
IsAlphaB_pFb = IsCharAlpha(AsciiChar)
End Function

This gives you two different ways of passing the char:
The one way is to pass a literal character.
The other way is to pass an ASCII character code (such as when passing the character code from a KeyDown or KeyPress event)

There is another API function to check for AlphaNumeric (IsCharAlphaNumeric). And, between the two you can create a function to check just for numbers. Numbers in the sense of a single digit - no decimals:
If the IsCharAlphaNumeric returns TRUE and the IsCharAlpha returns FALSE, then the char is a number.

So, in a Key press event that should allow only numbers, you can check if the char is a negative sign, or a decimal separater first, and if not then pass the code to your IsAlphaNumeric function and it will determine if the char is a number. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top