Solved

How to validate textbox for email in vb.net

Posted on 2012-04-05
5
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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