Solved

How to validate textbox for email in vb.net

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

16 Experts available now in Live!

Get 1:1 Help Now