Accueil > > > CLASSE DE VALIDATION
CLASSE DE VALIDATION
Information sur la source
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
- ''' -----------------------------------------------------------------------------
- ''' 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
''' -----------------------------------------------------------------------------
''' 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 ...
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
[ASP.NET]validation d'un champ email [ par shadow1779 ]
Bonjour, j'essaye de vérifier un formulaire avant de faire le postback ca marche pour tous les champs sauf l'adresse email, quelqu'un saurrait t'il po
Prob d'envoi d'email avec CDO [ par allserv ]
Bonjour tout le monde, Voici mon problème, j'ai une erreur lors de l'envoi d'un email avec CDO, (connexion perdue avec le serveur) la taille du corp
Validation d'une date [ par alexandre_69 ]
Salut !J'ai un ptit souci avec la validation d'une date en asp.net (c#) ! Jai essayé avec un compare validator sur mon champ de texte ! Je souhaiterai
Problème envois mail avec iiS 7.0 [ par gatita_dev ]
Bonjour, j'essaye d'envoyer un email en utilisant asp.net via mon serveur local IIS, j'ai configuré le SMTP comme suitNom du serveur : LocalHostPort d
validation d'une adresse IP avec masque asp.net [ par meryad ]
Bonjour,Je voudrais vérifier dans un champ d'un formulaire si une adresse IP avec masque est valide ou pas.J'ai trouvé le code de validation d'une adr
envoyer un email en asp/c# [ par bain ]
Bonjour j'ai un problème au niveau de mon envoi d'email.Voici la partie de mon fichier .aspx.cs qui concerne ce point:using System.Web.Mail; Ma
RegularExpressionValidator ne vérifie pas toujours le champ à valider [ par Alexandre Marlot ]
Bonjour à tous.J'ai contaté que lorsque j'utilise un RegularExpressionValidator sur un champ Email et que le navigateur propose une liste de choix pou
enableviewstate erreur [ par sasmami ]
bonjour,je developpe en asp.net avec vs2005. et dans une aspx j'ai un gridview avec des boutons. dés que je clique sur un de ces boutons j'obtien l'er
email [ par le_roi_hidri ]
je veux faire une tache planifier pour envoyer des fichier vers une adresse email .sans que j'interviens . a tenir compte que mon ordinateur et connec
Validation Control [ par blueangel ]
Salut tout le monde,voici mon code<inputid<font color="#0000ff" size
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
GRIDVIEW CHECKBOXGRIDVIEW CHECKBOX par invent001
Cliquez pour lire la suite par invent001 OUTIL MYSQLOUTIL MYSQL par nobla
Cliquez pour lire la suite par nobla
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|