This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class InputBoxValidated | |
Public Function InputBoxValidated _ | |
( | |
prompt As String, | |
title As String, | |
Optional stringValidator As IStringValidator = Nothing, | |
Optional validationMessage As String = "" | |
) As String | |
Dim value As String _ | |
= Microsoft.VisualBasic.Interaction.InputBox(prompt, title) | |
' If the cancel button wasn't pressed | |
' And IStringValidator is passed with validation message | |
If Not value = String.Empty AndAlso stringValidator IsNot Nothing _ | |
AndAlso Not String.IsNullOrEmpty(validationMessage) Then | |
If Not stringValidator.Validate(value) Then | |
MessageBox.Show(validationMessage, Application.ProductName) | |
value = InputBoxValidated( | |
prompt, title, stringValidator, validationMessage) | |
End If | |
End If | |
Return value | |
End Function | |
End Class |
As you can see InputBoxValidated function uses IStringValidator interface that can implement any kind of string validations and criteria.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Interface IStringValidator | |
Function Validate(value As String) As Boolean | |
End Interface |
Below is an example of StringValidator class that implements IStringValidator interface and makes use of StringValidator from System.Configuration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class StringValidator | |
Implements IStringValidator | |
Public Sub New(maxLength As Integer, invalidCharacters As String) | |
Me.MaxLength = maxLength | |
Me.InvalidCharacters = invalidCharacters | |
End Sub | |
Public Property MaxLength As Integer | |
Public Property InvalidCharacters As String | |
Public Function Validate(value As String) _ | |
As Boolean Implements IStringValidator.Validate | |
Dim valid As Boolean = True | |
Try | |
Dim stringValidator As _ | |
New System.Configuration.StringValidator _ | |
(0, MaxLength, InvalidCharacters) | |
If stringValidator.CanValidate(value.GetType()) Then | |
stringValidator.Validate(value) | |
Else | |
valid = False | |
End If | |
Catch ex As ArgumentException | |
valid = False | |
End Try | |
Return valid | |
End Function | |
End Class |
It's usefull blog,Thanks for sharing
ReplyDeleteDot Net Online Course Hyderabad