Regex is the cleanest way of doing this. However you asked for the long way...
This works by removing all upper and lowercase letters from a string - leaving behind anything else. If we then see how long stringname.length
the string is after the removal has completed, and find the number is zero then the validation has passed. However if the number is greater than zero, then our string contained non alphabet characters.
If (TextBox1.Text <> "") Then
Dim userInput As String = TextBox1.Text
Dim filteredUserInput As String = userInput
Dim listOfLetters As String() = New String() {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
' go through each letter in the 'listOfLetters' array and replace that letter with.. nothing
For Each letter In listOfLetters
filteredUserInput = Replace(filteredUserInput, letter, "")
Next
' now we have done the work - count how many characters are left in the string, if it is more than 0 we have invalid characters
If (filteredUserInput <> "") Then
MsgBox("This failed validation, contains invalid chars (" + Str(filteredUserInput.Length) + ")")
Else
MsgBox("This passed validation")
End If
End If
Or if you want it function-ified..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If (TextBox1.Text <> "") Then
Dim userInput As String = TextBox1.Text
' if the 'isThisAValidString()' returns true then it is a valid (and has not faild validation) a-zA-Z
If (isThisAValidString(userInput) = True) Then
MsgBox("This is valid")
Else
MsgBox("This is not valid")
End If
End If
End Sub
Function isThisAValidString(input As String)
Dim userInput As String = input
Dim filteredUserInput As String = userInput
Dim listOfLetters As String() = New String() {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
' go through each letter in the 'listOfLetters' array and replace that letter with.. nothing
For Each letter In listOfLetters
filteredUserInput = Replace(filteredUserInput, letter, "")
Next
' now we have done the work - count how many characters are left in the string, if it is more than 0 we have invalid characters
If (filteredUserInput <> "") Then
' this failed!
Return False
Else
'this passed
Return True
End If
End Function