I want to define a RegEx validator function which checks every character while typing (and not only after entering whole string) based on a defined 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