Share via


How can I validate an email address? in Visual Basic

Question

Saturday, February 4, 2012 6:59 AM

Hi All

How can I validate an email address? in Visual Basic 2010

with regards

 

All replies (6)

Saturday, February 4, 2012 7:06 AM ✅Answered

Imports System.Text.RegularExpressions
'If Not Regex.Match("Your Email Address", "^[a-zA-Z0-9\w.]+[@]+[a-zA-Z0-9\w]+[.]+[a-zA-Z0-9\w]*$", RegexOptions.IgnoreCase).Success Then
MsgBox("Enter a valid e-mail address ")
'End If


Saturday, February 4, 2012 12:31 PM ✅Answered

Hi ..

I have used the reguler expresion for validating the strings.

A detailed explanation have from the following link.. check this

http://www.regular-expressions.info/email.html

Solutions have in this link.

http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/f75edcc0-2ab5-4041-a9a8-b3b3cadd71c7

http://social.msdn.microsoft.com/Forums/en/regexp/thread/06fead4d-3725-4172-aa1a-08f594431a04

http://www.codeproject.com/Articles/5189/Effective-Email-Address-Validation

http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial

In its current form, which is 

^([0-9a-zA-Z]([-\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\)+[a-zA-Z]{2,9})$

the regular expression has a number of limitations. You've mentioned its support for only a subset of supported characters (it fails to support ! # $ %& ' * + = ? ^ ` { } | and ~), but there are a few other limitations that are worth noting as well:

  1. The local-part of an email address can still contain invalid characters if it is delimited by quotation marks. The IsValidEmail method does not recognize this convention and returns False.
  2. Double dots in an email address are not allowed. The IsValidEmail method does not recognize this restriction and, if the email address is otherwise valid, returns True.
  3. It does recognize a domain that consists of an IP address. In such cases, it always returns False.
  4. It does not validate the top-level domain. For example, it would recognize as valid an address like [email protected].

Although we have revised the regular expression several times in the past, it seems in need of another update. This will be reflected in the next scheduled update of the Visual Studio 2008 documentation, as well as in the final Visual Studio 2010 documentation. A preliminary version of this regular expression is:

^(("".+?"")|([0-9a-zA-Z](((\.(?!\.))|([-!#\$%&'\*\+/=\?\^`\{\}\|~\w]))*[0-9a-zA-Z])*))@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$

It corrects three of the issues that I've listed above (it supports all valid characters listed in RFC 5322, recognizes a local-part that is delimited by quotation marks, and disallowed double dots). It still doesn't recognize IP addresses (we'll fix this in the revised documentation), nor does it validate the top-level domain (a regex is not the appropriate tool to do this). 

I hope it will usful one for you..

By

P Elayaraja

 

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"


Saturday, February 4, 2012 2:50 PM ✅Answered | 2 votes

You can try using regular expressions to validate an email address, see How to Find or Validate an Email Address, or you can do it the easy way:

Function IsValidEmailAddress(ByVal emailAddress As String) As Boolean
    Dim valid As Boolean = True
    Try
        Dim a = New System.Net.Mail.MailAddress(emailAddress)
    Catch ex As FormatException
        valid = False
    End Try
    Return valid
End Function

HTH,

Andrew


Saturday, February 4, 2012 7:32 AM

Hello,

Try the links below.

http://www.vbforums.com/showthread.php?t=407441

http://www.dreamincode.net/forums/topic/15555-how-can-i-validate-an-email-address/


Saturday, February 4, 2012 11:49 AM

Hello Jo Swa

thanks for your Fast Respond

I Tried the code below but get return..!

If Not Regex.Match(testmail@gmail., "^[a-zA-Z0-9\w.]+[@]+[a-zA-Z0-9\w]+[.]+[a-zA-Z0-9\w]*$", RegexOptions.IgnoreCase).Success Then
            MsgBox("Enter a valid e-mail address ")
Else
            MsgBox("Yes a valid e-mail address ")
End If

get return message "Yes a valid e-mail address "

"testmail@gmail." how is valid this?. not use com, net or like so

regards


Monday, February 6, 2012 6:07 AM

do you only want to validate using regex or you want to check whether it exist or not?
java