我需要在VB中验证用户输入,在那里用户写5个数字像这样的"### ##",3个数字,一个缺口和2个以上的数字。我怎么做才是最好的方法呢?
谢谢,迈克。
发布于 2012-03-12 02:10:55
正则表达式- "\d{3}\s\d{3}"
例如,使用RegularExpressionAttribute
Public Class Model
<RegularExpression( "^\d{3}\s\d{3}$", ErrorMessage:="You must enter the date as ### ##" )>_
Public Property Data as String
'Getter and setter logic
End Property
End Class使用Regex类
...
Public Function Validate( ByVal data as String ) as Boolean
Static ValidationRegex as Regex = new Regex("^\d{3}\s\d{3}$",RegexOptions.Compiled)
Validate = ValidationRegex.IsMatch( data )
End Function我认为这在VB6中是可能的,但没有为您提供示例。
发布于 2012-03-12 02:19:55
让您的代码自动格式化字符串。这使得用户可以更容易地使用
Dim s As String
Dim n As Integer
s = textBox1.Text.Replace(" ","") ' Remove spaces
If s.Length = 5 AndAlso Int32.TryGetValue(s, n) Then
textBox1.Text = n.ToString("000 00")
Else
MessageBox.Show("You must enter five digits!")
EndIf发布于 2012-03-12 05:07:23
可能过于简单化了,但我只会使用一个屏蔽的输入框。
https://stackoverflow.com/questions/9657461
复制相似问题