Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSee if a string contains only digits or only letters
Keywordsstring, numeric, alpha, digits, letters
CategoriesStrings
 
This example uses two functions, AllDigits and AllLetters, that return True if a string contains only digits/letters. They examine each character to see if they fall within the allowed range.
 
' Return True if the string contains only digits.
Private Function AllDigits(ByVal txt As String) As Boolean
Dim ch As String
Dim i As Integer

    AllDigits = True
    For i = 1 To Len(txt)
        ' See if the next character is a non-digit.
        ch = Mid$(txt, i, 1)
        If ch < "0" Or ch > "9" Then
            ' This is not a digit.
            AllDigits = False
            Exit For
        End If
    Next i
End Function

' Return True if the string contains only letters.
Private Function AllLetters(ByVal txt As String) As Boolean
Dim ch As String
Dim i As Integer

    AllLetters = True
    txt = UCase$(txt)
    For i = 1 To Len(txt)
        ' See if the next character is a non-digit.
        ch = Mid$(txt, i, 1)
        If ch < "A" Or ch > "Z" Then
            ' This is not a letter.
            AllLetters = False
            Exit For
        End If
    Next i
End Function
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated