vb.net 如何验证 Vb 中的文本框只允许 3 个数字和 2 个字母?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19242218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:23:52  来源:igfitidea点击:

How to validate a textbox in Vb to only allow 3 numbers and 2 letters?

vb.netcharacternumeric

提问by Philip Dyson

I figured out how too get only numbers in a text box with is code:

我想出了如何在带有代码的文本框中只获取数字:

 Dim smessage As String = String.Empty

 If Not IsNumeric(Student_IDTextBox.Text) Then
      smessage += "The ID must be Numeric!" + Environment.NewLine
 End If

But I would like this textbox to have 2 letters and 3 numbers, do you know what the best way to programme this in vb?

但我希望这个文本框有 2 个字母和 3 个数字,你知道在 vb 中编程的最佳方法是什么吗?

采纳答案by Pow-Ian

This is certainly not the bestway, but I do not know if you will ever find the bestway because bestis subjective and often dependent on more than one situation as well as opinion.

这当然不是最好的方法,但我不知道你是否会找到最好的方法,因为最好是主观的,而且往往取决于不止一种情况和意见。

Assuming a text box with the name txtinputand a label where you will be displaying the results named lblMessageand assuming you are using ASCII character input:

假设有一个带有名称txtinput和标签的文本框,您将在其中显示命名的结果lblMessage并假设您使用的是 ASCII 字符输入:

In the TextChangedevent of txtinputyou could have the following:

TextChanged的情况下txtinput,你可以有以下几种:

'Check if the length is greater than five, if it is truncate it.
If txtinput.Text.Length > 5 Then
    txtinput.Text = Mid(txtinput.Text, 1, 5)
    txtinput.Select(txtinput.Text.Length, 0)
End If

'counters for letters and numbers
Dim letters As Integer = 0
Dim numbers As Integer = 0

'Parse and compare the input
For Each c As Char In txtinput.Text
    If Asc(c) >= 48 And Asc(c) <= 57 Then 'ASCII characters for 0-9
        numbers += 1
    ElseIf Asc(c) >= 65 And Asc(c) <= 90 Then 'ASCII characters for A-Z
        letters += 1
    ElseIf Asc(c) >= 97 And Asc(c) <= 122 Then 'ASCII characters for a-z
        letters += 1
    End If
Next

If letters = 2 And numbers = 3 Then
    lblMessage.Text = "Correct Format"
Else
    lblMessage.Text = "Incorrect Format"
End If

Using Linq:

使用 Linq:

If txtinput.Text.Length > 5 Then
    txtinput.Text = Mid(txtinput.Text, 1, 5)
    txtinput.Select(txtinput.Text.Length, 0)
End If

If txtinput.Text.Count(Function(x As Char) Char.IsLetter(x)) = 3 And txtinput.Text.Count(Function(x As Char) Char.IsNumber(x)) = 2 Then
    lblMessage.Text = "Correct Format"
Else
    lblMessage.Text = "Incorrect Format"
End If

回答by jyothis

please try masked textbox with custom mask. set mask like- LLL00. refer this link http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

请尝试使用自定义蒙版的蒙版文本框。像 LLL00 一样设置掩码。请参阅此链接 http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

回答by ??ssa P?ngj?rdenlarp

If the ID must be 3 numerals and 2 characters than there is probablyalso a pattern to it (as with many license plates) and more important than the mere character type count. A masked text box is one way, counting the numerals and counting the letters is another.

如果 ID 必须是 3 个数字和 2 个字符,那么它可能还有一个模式(就像许多车牌一样)并且比单纯的字符类型计数更重要。屏蔽文本框是一种方法,计算数字和计算字母是另一种方法。

If there is a pattern such as AAA-NN or AAANN, you could split the ID into 2 inputs one alpha, one numeric. This is often done with IDs in patterns such as a (US) social security number (NNN-NN-NNNN). RegEx could also probably be used to test the pattern.

如果存在诸如 AAA-NN 或 AAANN 之类的模式,您可以将 ID 分成 2 个输入,一个是字母,一个是数字。这通常使用模式中的 ID 来完成,例如(美国)社会安全号码 (NNN-NN-NNNN)。RegEx 也可能用于测试模式。

If this is a login or otherwise a database app, rather than write too much code to simply test the patterntest the entry. You could collect whatever they type and do a simple query to see if the ID exists, which after all is far more important than the pattern.

如果这是登录或其他数据库应用程序,而不是编写太多代码来简单地测试模式测试条目。您可以收集他们输入的任何内容并执行简单的查询以查看 ID 是否存在,这毕竟比模式重要得多。

A label on the form can tell them to use ###AA or whatever, but it seems silly to test the pattern and report a pattern error when you can simply tell them when it is invalid. After all, even if it has the correct pattern, it can still be an invalid ID.

表单上的标签可以告诉他们使用###AA 或其他什么,但是当您可以简单地告诉他们无效时,测试模式并报告模式错误似乎很愚蠢。毕竟,即使它有正确的模式,它仍然可能是一个无效的 ID。