正規表現は、これを行う最もクリーンな方法です。しかし、あなたは長い道のりを求めました...
これは、文字列からすべての大文字と小文字を削除することで機能し、それ以外はすべて残します。削除が完了した後の文字列の長さstringname.length
を確認し、数値がゼロであることがわかった場合、検証は成功です。ただし、数値が 0 より大きい場合、文字列にはアルファベット以外の文字が含まれていました。
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
または、機能化したい場合..
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