Could someone please tell me hw 2 validate e-mail address on an event in vb.net??

Recommended Answers

All 6 Replies

So there are a couple of ways to validate the format of an email address.

You could always to the simple look for the @ sign and make sure there is a dot (.) somewhere after the @ sign.

Or the kind of cool and groovy method of using Regular Expressions (RegEx).

Here's how you do it:
Add a textbox (Textbox1) and a button (Button1) to a form.

Switch to code view.

Add the imports statement

Imports System.Text.RegularExpressions

Then in your Button1_Click Event add the following code:

Dim strMessage As String = ""
        Dim regex As Regex = New Regex("([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\." + _
                                       ")|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", _
                                       RegexOptions.IgnoreCase _
                                       Or RegexOptions.CultureInvariant _
                                       Or RegexOptions.IgnorePatternWhitespace _
                                       Or RegexOptions.Compiled _
                                       )
        Dim IsMatch As Boolean = regex.IsMatch(TextBox1.Text)
        If IsMatch Then
            If TextBox1.Text.Equals(regex.Match(TextBox1.Text).ToString) Then
                strMessage = "Valid email address"
            Else
                strMessage = "There's an email address in there somewhere.  But not exactly"
            End If
        Else
            strMessage = "Sorry.  Invalid email address format."
        End If
        MessageBox.Show(strMessage)

By the way there is no good way to tell if an address actually exists (or is valid) until you get a response back from the server telling you it doesn't exists. Unless of course you're the system administrator of that particular email server.

If this solved your problem, please mark the thread as solved.

Thanks.

thanks a lot.. bt i got it done!! :)

As I have entered this code in my project
but with this when I click on the save button it shows me the

strMessage = "Sorry.  Invalid email address format."

but it also saves into my database
how can I prevent that?

First u have to validate all the conditions and if all the conditions are passed then save to database....

Like

if emailvalidation =  false or other things like name and so on then 
    do this
else 
    save to database
Private Sub TextBox5_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox5.Validating
    If Regex.Match(TextBox5.Text, "^[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]$").Success Then
        MsgBox("Valid Email", MsgBoxStyle.Information, Title:="HRMS")
    Else
        MsgBox("Invalid Email", MsgBoxStyle.Information, Title:="HRMS")
        TextBox5.Text = ""
    End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.