【发布时间】:2016-04-28 03:47:37
【问题描述】:
我想定义一个 RegEx 验证器函数,它根据定义的 RegEx在键入时检查每个字符(而不仅仅是在输入整个字符串之后):
Public Class RegExValidator
Inherits System.Windows.Forms.TextBox
Private regex As New System.Text.RegularExpressions.Regex("[1-9-]\/[1-9-][\-+][abo][\/]some\-number\:\d{1,5}")
Public Function CheckUserInput(ByVal input As String) As Boolean
' this only checks whole input so for
'beggining parts of input it returns
'false and protects users to finish
'their correct input
If regex.IsMatch(input) Then
Return True
Else
' should check if currect input is matching
'beggining parts of RegEx and if not matches
'some part of RegEx return false
'else return true
End If
End Function
' I'm going to override PreProccessMessage()
'on real application but this is just a
'sample to demonstrate my problem simply
Protected Overrides Sub OnTextChanged(e As EventArgs)
MyBase.OnTextChanged(e)
If Not CheckUserInput(Me.Text) Then
MsgBox("Validation failed! This character is not allowed!")
Me.Text = Me.old_text
End If
End Sub
Private old_text As String
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(value As String)
Me.old_text = Me.Text
MyBase.Text = value
End Set
End Property
End Class
【问题讨论】:
-
@halfer 好的,如果您喜欢这种方式,没问题 :) 如果我第一次无视您的帮助,请原谅。感谢您的关心。
-
好的,谢谢雷蒙!
-
@halfer 不客气。
标签: .net regex vb.net validation