我只希望在vb.net上的文本框中接受信件

时间:2013-01-04 17:13:02

标签: vb.net string validation textbox character

我一直在尝试这样做,因此文本框只接受字母,但验证不起作用。 即使我输入数字,它也会处理它并显示“谢谢你的详细信息”的第一个lblError,它实际上应该是“输入有效名称”。 对于这类问题,他们的验证测试类似于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模式,因为我已经研究了这些,我不能使用它们。

三江源

6 个答案:

答案 0 :(得分: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事件。

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分:0)

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

enter code here 

textbox1.textchanged

Dim check,check2 as Boolean

check = textbox1.text like&#34; [A-Za-z]&#34; &#39;检查信件 check2 = textbox1.text like&#34; [0-9]&#34; &#39;检查不是字母

如果check = true,则check2 = false 附加文字 elseif check = false或check2 = true然后 不要追加文字

希望有所帮助

答案 4 :(得分: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

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

答案 5 :(得分:0)

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

其次,Unicode很复杂。特别是,.NET Strings使用UTF-16代码单元,其中一个或两个代码单元对代码点进行编码。此外,一些代码点是&#34;组合字符&#34; - 它们不能独立存在,但经常单独出现或在字母后面相乘。

以下是验证逻辑。它遍历字符串并检查每个文本元素(aka grapheme)的第一个代码点是否为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

BTW-你对名字的看法非常狭隘。我扔进一个空间来打破一个误解;这是一个明显的例子。但是,一般来说,我不想告诉用户我认为他们的名字无效。