How to check whether email address valid or not in asp.net

Introduction: In this article i have explained how to check whether email address valid or not in asp.net.
In previous articles i explained How to show Validation guidelines in web forms using JavaScript in Asp.net and  How to implement JavaScript validation in asp.net website  and Count and display remaining characters in the multiline textbox in asp.net  and  How to enable JavaScript in asp.net using C#,VB.Net and  How to use CustomValidator validation control with example in asp.net and all other asp.net validation controls provided by visual studio.

Description: It is often required to get and store user email through contact us form, login form etc. But it is also highly important to check the validity of the Email address submitted by the user before storing to ensure that the email address is valid.

Implementation: Let's check it out using an example


Asp.Net C# Code to check whether email address valid or not

First Include the namespace as:

using System.Text.RegularExpressions;

then write the code as:

public bool IsEmailValid(string Email)
    {
        string strRegex = "^([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})(\\]?)$";
        Regex re = new Regex(strRegex);
        if (re.IsMatch(Email))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
  • Now just call this function and pass the email address as a parameter to check whether it is valid or not. e.g. on Button Click event as:
protected void btnEmailCheck_Click(object sender, EventArgs e)
    {
        if (IsEmailValid("TestEmail@domain.com"))
        {
            Response.Write("Valid Email Id");
        }
        else
        {
            Response.Write("InValid Email Id");
        }
    }


Asp.Net VB Code to check whether email address valid or not

First import namespace as:

imports System.Text.RegularExpressions

Public Function IsEmailValid(Email As String) As Boolean
 Dim strRegex As String = "^([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})(\]?)$"
 Dim re As New Regex(strRegex)
 If re.IsMatch(Email) Then
  Return True
 Else
  Return False
 End If
End Function
  • Now just call this function and pass the email address as a parameter to check whether it is valid or not. e.g. on Button Click event as:
Protected Sub btnEmailCheck_Click(sender As Object, e As EventArgs) Handles btnEmailCheck.Click
       If IsEmailValid("TestEmail@domain.com") Then
            Response.Write("Valid Email Id")
        Else
            Response.Write("InValid Email Id")
        End If
    End Sub

Now over to you:

" I hope you have got the way to check the validity of email address and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..