Validar email

27/07/2004 - 22:54 por CRISTHIAN MASSA MEDINA | Informe spam
Hola amigos he encontrado esta validacion de email, espero les sirva

Function ChkValidEmail(ByVal Value As String, Optional ByVal MaxLength As _
Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean
If Value Is Nothing OrElse Value.Length = 0 Then
' rule out the null string case
Return Not IsRequired
ElseIf Value.Length > MaxLength Then
' rule out values that are longer than allowed
Return False
End If

' search invalid chars
If Not System.Text.RegularExpressions.Regex.IsMatch(Value, _
"^[-A-Za-z0-9_@.]+$") Then Return False

' search the @ char
Dim i As Integer = Value.IndexOf("@"c)
' there must be at least three chars after the @
If i <= 0 Or i >= Value.Length - 3 Then Return False
' ensure there is only one @ char
If Value.IndexOf("@"c, i + 1) >= 0 Then Return False

' check that the domain portion contains at least one dot
Dim j As Integer = Value.LastIndexOf("."c)
' it can't be before or immediately after the @ char
If j < 0 Or j <= i + 1 Then Return False

' if we get here the address if validated
Return True
End Function
 

Leer las respuestas

#1 Sergio Florez M.
27/07/2004 - 23:06 | Informe spam
Es valioso tu aporte pero creo que es mas limpio y eficiente hacerlo con un
RegularExpressionValidator. Solo escoges el textbox que quieres validar,
asignas un mensaje de error y listo. De hecho el Visual Studio incluso trae
una expresion para validar e-mails y es esta:
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*


Sergio Florez M.
Miembro activo www.AlianzaDev.net
El que persevera insiste
Medellín, Colombia


"CRISTHIAN MASSA MEDINA" <news.microsoft.com11> escribió en el mensaje
news:
Hola amigos he encontrado esta validacion de email, espero les sirva

Function ChkValidEmail(ByVal Value As String, Optional ByVal MaxLength As


_
Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean
If Value Is Nothing OrElse Value.Length = 0 Then
' rule out the null string case
Return Not IsRequired
ElseIf Value.Length > MaxLength Then
' rule out values that are longer than allowed
Return False
End If

' search invalid chars
If Not System.Text.RegularExpressions.Regex.IsMatch(Value, _
"^[]+$") Then Return False

' search the @ char
Dim i As Integer = Value.IndexOf("@"c)
' there must be at least three chars after the @
If i <= 0 Or i >= Value.Length - 3 Then Return False
' ensure there is only one @ char
If Value.IndexOf("@"c, i + 1) >= 0 Then Return False

' check that the domain portion contains at least one dot
Dim j As Integer = Value.LastIndexOf("."c)
' it can't be before or immediately after the @ char
If j < 0 Or j <= i + 1 Then Return False

' if we get here the address if validated
Return True
End Function


Preguntas similares