Validate Email Id on server Side using RegEx in ASP.NET

Aman Sharma
0
In this article I will explain how to validate Email ID on server side (entered by user).
We can validate email address on client side by using validations controls provided in Asp.NET. But they will not work if Javascript is disabled. In this case server side validation will work and more reliable.
In this article we will validate Email id using RegEx.

Demo:
Click here to Enlarge Image

Follow these steps to Validate Email Id Using RegEx on server side:

Design your webpage:

<fieldset style="width:370px"><legend>Display Data in Detail View Control</legend>
            <br />
        <table style="width:100%">
            <tr><td>Enter Email:</td><td>
                <asp:TextBox ID="txtEmail" runat="server" Width="200px"></asp:TextBox>
                </td></tr>
             <tr><td></td><td></td></tr>
             <tr><td></td><td>
                 <asp:Button ID="btnEmail" runat="server" OnClick="btnEmail_Click" Text="Validate Email" />
                 </td></tr>
             <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
             <tr><td>Result:</td><td>
                 <asp:Label ID="lblresult" runat="server"></asp:Label>
                 </td></tr>
        </table>
        <br /></fieldset>


 ASP.NET code Using C# to validate EmailID:

First include the namespace as:

using System.Text.RegularExpressions;


Write Following function to validate Email:

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 btnEmail_Click(object sender, EventArgs e)
    {
        if (IsEmailValid(txtEmail.Text))
        {
           lblresult.Text="This Email Id is Valid";
           lblresult.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblresult.Text = "This Email Id is InValid";
            lblresult.ForeColor = System.Drawing.Color.Red;
        }
    }



VB.NET 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(txtEmail.Text) Then
        Response.Write("Valid Email Id")
    Else
        Response.Write("InValid Email Id")
    End If

End Sub




Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !