Solved

How to validate textbox for email in vb.net

Posted on 2012-04-05
5
430 Views
Last Modified: 2012-04-24
I have vb.net windows form.
A textbox txtemail is there.
The email is optional.
But i want once email is being entered it should validate correct email address.
0
Comment
Question by:searchsanjaysharma
5 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 37813770
http://www.vbforums.com/showthread.php?t=407441

Imports System.Text.RegularExpressions

    Function EmailAddressCheck(ByVal emailAddress As String) As Boolean

        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            EmailAddressCheck = True
        Else
            EmailAddressCheck = False
        End If

    End Function
0
 
LVL 5

Expert Comment

by:truinx
ID: 37813773
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37813796
You can use Regex
Dim ErP As New ErrorProvider
If TextBox1.Text <> "" Then
    Dim rex As Match = Regex.Match(Trim(TextBox1.Text), "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase)
    If rex.Success = False Then
        ErP.SetError(TextBox1, "Please Enter a valid Email-Address")
    Else
        ErP.SetError(TextBox1, String.Empty)
    End If
End If

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37813801
Ah the age old question:  "How do I validate email addresses..."  You can really only validate structure. There is no way in code to tell if an email address is valid or not. Even if you attempt to send a message, the server is the entity which will receive the response. If for some reason you need to know whether or not the actual address is "live", then you will need to come up with some scheme to verify (maybe check against AD if you are on a LAN).

As far as checking structure, the typical solution is to run the value against a regular expression (regex). In the link I referenced, you will see a few different examples of expressions you might use. Each has benefits and drawbacks. If you decide to use regex for this purpose, then whichever pattern you use, you can use logic such as this to validate the address:

Dim emailIsValid As Boolean = System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "[pattern you chose]")

Open in new window


Again, the regex approach only validates structure. It cannot tell you whether or not an account, or even a domain exists.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37885160
ok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now