Solved

How to validate textbox for email in vb.net

Posted on 2012-04-05
5
436 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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