4

I've tried implementing a rather simple email validation function that seems return a false match even though the input is a valid email. I have searched for any issues with the existing regex but it seems to be correct.

Even though the match returns a false value the program is stepping to the next validation level (which it shouldn't).

Here is the email validation function.

Function EmailAddressChecker(ByVal emailAddress As String) As Boolean
        Dim regExPattern As String = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, regExPattern)
        If emailAddressMatch.Success Then
            Return True
        Else
            Return False
        End If
End Function

And for the form validation that calls upon the email validation function.

If (String.IsNullOrEmpty(EmailTextBox.Text) OrElse EmailAddressChecker(EmailTextBox.ToString)) Then
            MessageBox.Show("Please enter a valid email addresss")
            Return False
End If

The call for all of this happens on an click event which triggers a cascading serious of If statements checking to see if all the fields are set.

Skipping a large chunk of code the click event asks if "AreFieldsSet <> True". Inside of the "AreFieldsSet" function contains all the validation for multiple inputs; one of which is the email validation if statement.

2
  • We need to see the context this is called in - that's likely where the issue is. Commented Apr 17, 2012 at 17:21
  • You may also want to consider instantiating a System.Net.Mail.MailAddress object in a try catch for e-mail address validation. Commented Apr 17, 2012 at 18:18

4 Answers 4

4

Are the emails in UpperCase? If they aren't, they won't match.

If you want to modify the Regex so that it is Case insensitive, use this:

"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
Sign up to request clarification or add additional context in comments.

Comments

0

To validate the email address you need to use the IsMatch function of the Regex object, it evaluate if the entry email address is valid.

Function EmailAddressChecker(ByVal emailAddress As String) As Boolean
   Dim r As System.Text.RegularExpressions.Regex = Nothing
    Dim regExPattern As String = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"

    If r.IsMatch(emailAddress ,regExPattern ) Then
        Return True
    Else
        Return False
    End If
End Function

3 Comments

I haven't tested this, but isn't this functionally equivalent to what the original code does? Less code, but both should do the same thing.
I did notice however that the email address is equal to ""System.Windows.Forms.TextBox, Text: abc@aosdf". This obviously needs to be dealt with. I think I had a problem with this before.. Fix is something like "public override string ToString()" ????
Accessing the textbox via txtMyTextbox.Text should return the email string in its purest form. Calling txtMyTextBox.ToString() should return the string you provided "System.Windows.Forms.TextBox, Text: abc@aosdf".
0

You can try this code for your form validation If (String.IsNullOrEmpty(EmailTextBox.Text) OrElse EmailAddressChecker(EmailTextBox.ToString)<>true) Then MessageBox.Show("Please enter a valid email addresss") Return False End If

Comments

0
Public Shared Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean    

       If emailAddress.IndexOf("@") > -1 Then
        If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) AndAlso emailAddress.Split(".").Length > 0 AndAlso emailAddress.Split(".")(1) <> "" Then
            errorMessage = ""
            Return True
        End If
    End If      
    Return False
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.