0

我一直在尝试这样做,所以文本框只接受字母,但验证不起作用。即使我输入数字,它也会处理它并显示“Thankyou for your details”的第一个 lblError,它实际上应该是“Enter A Valid Name”。对于此类问题,它们是类似于 IsNumeric 的验证测试吗?请帮助

    Dim MyName As String
    If txtMyName.Text Then
        MyName = txtMyName.Text
        lblError.Text = "Thankyou for your details"
    Else
        lblError.Text = "Enter A Valid Name "

    End If

End Sub

结束类

而且我需要简单的方法,不需要 [a-zA-Z0-9] 或 RegEx 模式,因为我研究了这些但我无法使用它们。

谢谢

4

6 回答 6

4

您可以检查文本字符串,即 textbox1.text,以确保它在 .Leave 事件中除了字母字符之外没有任何内容。例如,当用户切换到下一个控件时,这将捕获一个错误。您可以使用正则表达式来执行此操作(在此示例中导入 System.Text.RegularExpressions),或者您可以“手动”检查文本。

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
  If Not Regex.Match(TextBox1.Text, "^[a-z]*$", RegexOptions.IgnoreCase).Success Then
    MsgBox("Please enter alpha text only.")
    TextBox1.Focus()
  End If

End Sub

如果您想在按下非字母键时立即停止用户,您可以使用 TextChanged 事件而不是 .Leave 事件。

于 2013-01-04T17:17:25.157 回答
2

正则表达式是最干净的方法。但是你问了很长的路...

这通过从字符串中删除所有大写和小写字母来工作 - 留下任何其他内容。如果我们stringname.length在删除完成后查看字符串有多长时间,并发现数字为零,则验证通过。但是,如果数字大于零,则我们的字符串包含非字母字符。

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
于 2013-01-04T22:11:37.147 回答
1

为什么不是正则表达式?这是工作的正确工具,除非您在问题中遗漏了一些东西。

您可以像这样构建一个所有英文字母的数组(它是大写的),然后您可以检查名称中的所有字符是否都在数组中。

Private Shared Function IsLetters(s As String) As Boolean
    For Each c As Char In s.ToUpper().ToCharArray()
        If Not onlyLetters().Contains(c) Then
            Return False
        End If
    Next
    Return True
End Function

Private Shared Function onlyLetters() As Char()
    Dim strs = New List(Of Char)()
    Dim o As Char = "A"C
    For i As Integer = 0 To 25
        strs.Add(Convert.ToChar(o + i))
    Next

    Return strs.ToArray()
End Function
于 2013-01-04T17:33:04.627 回答
0

首先,您应该注意您使用的是 Unicode。

其次,Unicode 很复杂。特别是,.NET 字符串使用 UTF-16 代码单元,其中一个或两个编码一个代码点。此外,一些代码点是“组合字符”——它们不能独立存在,而是经常单独出现或在字母之后出现多个。

下面是验证逻辑。它遍历字符串并检查每个文本元素(又名字素)的第一个代码点是否为 Unicode 字母。

Dim input = "ØysteinRene"+ Char.ConvertFromUtf32(&H301) +"e Galois" 
'COMBINING ACUTE ACCENT' (U+0301)
Dim etor = System.Globalization.StringInfo.GetTextElementEnumerator(input)
While (etor.MoveNext()) 
    Dim grapheme = etor.GetTextElement()
    ' check the first codepoint in the grapheme 
    ' (others will only be "combining characters")
    If Not Char.IsLetter(grapheme,0) Then 
        Throw New Exception("Your input doesn't match my idea of a name at """  _
                             + grapheme + """")
    End If
End While

顺便说一句——你对名字的看法非常狭隘。我扔了一个空格来打破一个误解;这是一个明显的案例。但是,一般来说,我不想告诉用户我认为他们的名字无效。

于 2014-09-21T21:07:08.757 回答
0

这应该有助于它适用于任何其他文本输入,例如输入框(peudo)

enter code here 

textbox1.textchanged

暗检查,检查 2 为布尔值

check = textbox1.text like " [A-Za-z]" ' 检查字母 check2 = textbox1.text like " [0-9]" ' 检查非字母

如果 check = true 和 check2 = false 然后附加文本 elseif check = false 或 check2 = true 然后不附加文本

希望能帮助到你

于 2014-03-20T20:57:38.587 回答
0

你不能使用ASCII字符吗?像这样:

(在按键事件上)

If Asc(e.KeyChar) <> 8 Then
  If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 122 Then
  ' from 65 to 90 A - Z String is allowed ( Uppercase letter )
  ' from 97 to 122 a - z String is allowed ( Lowercase letter )
    If Asc(e.KeyChar) > 97 Or Asc(e.KeyChar) < 91 Or Asc(e.KeyChar) = 95 Then
  ' As we dont need to include letters between 91-96 we add this code. 
  ' ASCII CHARACTER 95 is Underscore Character.  so we add this manually.

      e.Handled = True

    End If
  End If
End If

如果您不需要允许“_”下划线然后删除“ Or Asc(e.KeyChar) = 95 您可以轻松地做到这一点。你应该看 ASCII 字符表来自己做。你可以在这里查看表格

于 2014-09-21T18:19:35.520 回答