Classe de validation

Description

Cette classe permet de Valider certaines données pour l'instant il n'y a pas grand chose, mais j'en rajouterais quand j'en aurais besoin, j'ai mis l'email qui peut etre interessantet les test sur les nombres

Je me suis fortement inspiré de la source de Fabrice69 : http://www.aspfr.com/code.aspx?ID=21154

Dans le zip vous avez une assembly contenant d'autre truc sur lequel je travail, pour avoir la dernier version de celle ci :
http://jesusonline.free.fr/Cyril/
Y'a la description de la classe ici : http://jesusonline.free.fr/Cyril/Commentaire%2002.Avril.2004/Solution_Cyril.HTM

Fonctionnement

Dim Valid As New Cyril.Utility.Validation.StringValidation
if Valid.isEmail("MonEmail@codes-sources.com") then
'Mail Valide'
end if

Source / Exemple :


''' -----------------------------------------------------------------------------
    ''' Project	 : Cyril
    ''' Class	 : Utility.Validation
    ''' 
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Desolé classe non commenté :(
    ''' 
    ''' Pour la realisation de celle ci je me suis fortement inspiré d'une source 
    ''' de Fabrice69 : http://www.aspfr.com/code.aspx?ID=21154
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[DURAND Cyril]	02/04/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class Validation

        Private _RegExp As String
        Public Property RegExp() As String
            Get
                Return _RegExp
            End Get
            Set(ByVal Value As String)
                _RegExp = Value
            End Set
        End Property

#Region "Constructeurs"

        Public Sub New()
        End Sub
        Public Sub New(ByVal PersonalRegexp As String)
            _RegExp = PersonalRegexp
        End Sub

#End Region

        Public Overridable Function isInteger(ByVal _Value As String) As Boolean
            Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?0*\d{1,10}$")
            If objIntPattern.IsMatch(_Value) = True Then
                If CType(_Value, Decimal) <= 2 ^ 31 - 1 And CType(_Value, Decimal) >= 2 ^ 31 Then
                    Return objIntPattern.IsMatch(_Value)
                End If
            Else
                Return False
            End If

        End Function

        Default Public Overloads ReadOnly Property isPersonnalExpression(ByVal _Value As String, ByVal PersonalRegexp As String) As Boolean
            Get
                If PersonalRegexp = Nothing Then
                    Throw New Exception("Absence de l'expression de validation")
                    Exit Property
                End If

                Try
                    Dim objIntPattern As New System.Text.RegularExpressions.Regex(RegExp)
                    Return objIntPattern.IsMatch(_Value)
                Catch ex As Exception
                    Throw New Exception("Expression de validation non valide : " & PersonalRegexp & ". L'erreur est : " & ex.Message, ex)
                    Exit Property
                End Try
            End Get
        End Property
        Default Public Overloads ReadOnly Property isPersonnalExpression(ByVal _Value As String) As Boolean
            Get
                If _RegExp = Nothing Then
                    Throw New Exception("Absence de l'expression de validation")
                    Exit Property
                End If

                Try
                    Dim objIntPattern As New System.Text.RegularExpressions.Regex(_RegExp)
                    Return objIntPattern.IsMatch(_Value)
                Catch ex As Exception
                    Throw New Exception("Expression de validation non valide : " & _RegExp & ". L'erreur est : " & ex.Message, ex)
                    Exit Property
                End Try
            End Get
        End Property

        Public Class StringValidation
            Inherits Validation

            Public Function isEmail(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$")
                Return objIntPattern.IsMatch(_Value)
            End Function

        End Class

        Public Class NumericValidation
            Inherits Validation

            Public Function isBin(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[0-1]+[0-1]*$")
                Return objIntPattern.IsMatch(_Value)
            End Function

            Public Function IsDec(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?\d{1,29}$")
                Return objIntPattern.IsMatch(_Value)
            End Function

            Public Function isInt32(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?0*\d{1,10}$")
                If objIntPattern.IsMatch(_Value) = True Then
                    If CType(_Value, Decimal) <= 2 ^ 31 - 1 And CType(_Value, Decimal) >= 2 ^ 31 Then
                        Return objIntPattern.IsMatch(_Value)
                    End If
                Else
                    Return False
                End If

            End Function

            Public Function isInt16(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?0*\d{1,5}$")
                If objIntPattern.IsMatch(_Value) = True Then
                    If CType(_Value, Decimal) <= 2 ^ 15 - 1 And CType(_Value, Decimal) >= 2 ^ 15 Then
                        Return objIntPattern.IsMatch(_Value)
                    End If
                Else
                    Return False
                End If

            End Function

            Public Function isSbyte(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?0*\d{1,3}$")
                If objIntPattern.IsMatch(_Value) = True Then
                    If CType(_Value, Decimal) <= 127 And CType(_Value, Decimal) >= -128 Then
                        Return objIntPattern.IsMatch(_Value)
                    End If
                Else
                    Return False
                End If

            End Function

            Public Function isByte(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^[-]?0*\d{1,3}$")
                If objIntPattern.IsMatch(_Value) = True Then
                    If CType(_Value, Decimal) <= 255 Then
                        Return objIntPattern.IsMatch(_Value)
                    End If
                Else
                    Return False
                End If

            End Function

            Public Function isHex(ByVal _Value As String) As Boolean
                Dim objIntPattern As New System.Text.RegularExpressions.Regex("^#?([a-f]|[A-F]|[0-9])*$")
                Return objIntPattern.IsMatch(_Value)
            End Function

            Public Class NumericConversion
                Inherits NumericValidation

                Public Function toInt32(ByVal _Value As String) As Integer
                    If Me.isInt32(_Value) = True Then
                        Return CType(_Value, Integer)
                    Else
                        Throw New Exception("Convertion impossible")
                    End If
                End Function
                Public Function toInt16(ByVal _Value As String) As Int16
                    If Me.isInt16(_Value) = True Then
                        Return CType(_Value, Integer)
                    Else
                        Throw New Exception("Convertion impossible")
                    End If
                End Function
                Public Function toByte(ByVal _Value As String) As Byte
                    If Me.isByte(_Value) = True Then
                        Return CType(_Value, Byte)
                    Else
                        Throw New Exception("Convertion impossible")
                    End If
                End Function
                Public Function toHexString(ByVal _Value As String) As String
                    If Me.isHex(_Value) = True Then
                        Return _Value
                    Else
                        Throw New Exception("Convertion impossible")
                    End If
                End Function
                Public Function toDec(ByVal _Value As String) As Decimal
                    If Me.IsDec(_Value) = True Then
                        Return CType(_Value, Decimal)
                    Else
                        Throw New Exception("Convertion impossible")
                    End If
                End Function

            End Class

        End Class

    End Class

Conclusion :


Cette classe est la pour évoluer, je serais ravis d'ajouter vos propre validation donc n'hésiter surtout pas à me faire part de vos création ...

dites moi ce que vous en pensez via les commentaires ...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.