Question Email Validation Problem??

zahir

Member
Joined
Apr 9, 2009
Messages
9
Programming Experience
1-3
I have copy the code from this forum but there are still some error in validate the "@" symbol in email,the email can always be valid if symbol "@" is none...can anyone help...

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim email As String = Me.email.Text.Trim.ToLower()
        Dim isValid As Boolean = True

        If email.Length >= 6 Then
            If email.CompareTo("@") Then
                If email.EndsWith(".com") OrElse email.EndsWith(".edu") OrElse email.EndsWith(".net") OrElse email.EndsWith(".org") Then
                    'Do Nothing
                Else
                    isValid = False
                End If
            Else
                isValid = False
            End If
        Else
            isValid = False
        End If

        If isValid = True Then
            MessageBox.Show("Address Valid")
        Else
            MessageBox.Show("Address Invalid")
        End If
    End Sub
 
This is what regular expressions is for: pattern matching. You could just validate the entire email pattern:
VB.NET:
Private Const m_RegexEmailPatternString As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Private m_EmailRegEx As New System.Text.RegularExpressions.Regex(m_RegexEmailPattern)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If m_EmailRegEx.IsMatch(Me.email.Text.Trim.ToLower()) = False Then
        'Not a valid email address
    End If
End Sub
 
I gave you everything you need already, all you need to do is finish it, which I can't do because your code doesn't do anything additional that mine doesn't (other than the message box, which you can easily put in)
 
i'm trying to implement that code and it cause an error that says name "g_RegexEmailPattern" is not been declare...

Can i know what is the declaration for this name and how to fix it...
 
i'm trying to implement that code and it cause an error that says name "g_RegexEmailPattern" is not been declare...

Can i know what is the declaration for this name and how to fix it...
I've fixed my code, the variable being passed to the constructor of the regex variable was wrong (I forgot to change the g to an m)
 
Back
Top