crjunk
crjunk

Reputation: 969

Validating Email Using System.Net.Mail.MailAddress Fails

I'm trying to validate a list of email addresses using the following code:

Public Function ValidateEmailAddressField() As Boolean
    Dim isValid As Boolean = True
    Try
        txtServiceEmails.Text = txtServiceEmails.Text.Trim.Replace(",", ";")
        Dim validateMailAddress = New MailAddress(txtServiceEmails.Text.Trim)
        Return isValid
    Catch ex As Exception
        isValid = False
        Return isValid
    End Try

End Function

When I enter "johndoe@amce" or "johndoe@acme, [email protected]" the code validates true. Is entering an email address without an extension, such as ".com", actually considered a valid email address?

Thanks,

crjunk

Upvotes: 0

Views: 2532

Answers (3)

crjunk
crjunk

Reputation: 969

Settled for a Regular Expression Validator solution.

Public Function ValidateEmailAddressField() As Boolean

    Dim isValid As Boolean = True
    Dim emailExpression As New Regex("^((\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}\s*[,;:]){1,100}?)?(\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})*$")
    Try
        txtServiceEmails.Text = txtServiceEmails.Text.Trim.Replace(",", ";")
        txtServiceEmails.Text = txtServiceEmails.Text.Trim.Replace(" ", "")
        isValid = emailExpression.IsMatch(txtServiceEmails.Text.Trim)

        Return isValid

    Catch ex As Exception
        isValid = False
        Return isValid
    End Try

End Function

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68440

Yes, it is valid. The domain part of email address must match the requirements for a hostname and depending of local configurations the "extension" may be optative.

A hostname is considered to be a fully qualified domain name (FQDN) if all the labels up to and including the top-level domain name (TLD) are specified. The hostname en.wikipedia.org terminates with the top-level domain org and is thus fully qualified. Depending on the operating system DNS software implementation, an unqualified hostname such as csail or wikipedia may be automatically combined with default domain names configured into the system, in order to determine the fully qualified domain name. As an example, a student at MIT may be able to send mail to "joe@csail" and have it automatically qualified by the mail system to be sent to [email protected].

Source: Wikipedia

Upvotes: 0

urlreader
urlreader

Reputation: 6615

In an cooperation environment, a domain does not have to have .com/net/org... 'acme' could be a valid domain, so, the email me@acme could be a valid email address internally.

usually, people use regular expression to valid email address. there are lots of examples.

Upvotes: 1

Related Questions