简体   繁体   English

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

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

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编程的最佳方法是什么?

please try masked textbox with custom mask. 请尝试使用自定义蒙版屏蔽的文本框。 set mask like- LLL00. 设置类似LLL00的蒙版。 refer this link http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx 请参阅此链接http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

If the ID must be 3 numerals and 2 characters than there is probably also a pattern to it (as with many license plates) and more important than the mere character type count. 如果ID必须是3个数字和2个字符,则可能还有一个模式(对于许多车牌来说),并且比单纯的字符类型计数更重要。 A masked text box is one way, counting the numerals and counting the letters is another. 带遮罩的文本框是一种方法,对数字和字母进行计数是另一种方法。

If there is a pattern such as AAA-NN or AAANN, you could split the ID into 2 inputs one alpha, one numeric. 如果存在AAA-NN或AAANN之类的模式,则可以将ID分成2个输入,一个字母,一个数字。 This is often done with IDs in patterns such as a (US) social security number (NNN-NN-NNNN). 通常使用ID(例如(美国)社会保险号(NNN-NN-NNNN))的方式来完成此操作。 RegEx could also probably be used to test the pattern. RegEx也可能用于测试模式。

If this is a login or otherwise a database app, rather than write too much code to simply test the pattern test 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. 表单上的标签可以告诉他们使用### AA或其他任何东西,但是当您可以简单地告诉他们无效时,测试模式并报告模式错误似乎很愚蠢。 After all, even if it has the correct pattern, it can still be an invalid ID. 毕竟,即使它具有正确的模式,它仍然可以是无效的ID。

This is certainly not the best way, but I do not know if you will ever find the best way because best is subjective and often dependent on more than one situation as well as opinion. 这当然不是最好的方法,但是我不知道您是否会找到最好的方法,因为最好是主观的,通常取决于不止一种情况和观点。

Assuming a text box with the name txtinput and a label where you will be displaying the results named lblMessage and assuming you are using ASCII character input: 假设一个名为txtinput的文本框和一个标签,您将在其中显示名为lblMessage的结果,并假设您使用的是ASCII字符输入:

In the TextChanged event of txtinput you could have the following: txtinputTextChanged事件中,您可以具有以下内容:

'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM