ASP.Net – Regular Expression to validate email id in C#.net

ASPX Code:

<!DOCTYPE html>
<html>
<head id=”Head1″runat=”server”>
    <title>ASP.Net – Regular Expression To Validate Email Address in C#.Net</title>
</head>
<body>
    <form id=”frmValidate” name=”frmValidate” runat=”server”>
    <div>
        <table border=”1″ cellpadding=”5″>
            <tr>
                <td>
                    Enter Email Id Here
                </td>
                <td>
                    <asp:TextBox ID=”txtEmailId” runat=”server”></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID=”btnValidateEmail” runat=”server” Text=”Check”OnClick=”btnValidateEmail_Click”/>
                </td>
            </tr>
            <tr>
                <td colspan=”2″>
                    <p style=”color: Red;”>
                        <asp:Label ID=”lblValidEmail” runat=”server”></asp:Label></p>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 C#.Net Code
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Text.RegularExpressions;
public partial class ValidateEmail : System.Web.UI.Page
{
    protected void btnValidateEmail_Click(objectsender, EventArgs e)
    {
        //Check Email Id is valid
        if(IsValidEmailId(txtEmailId.Text))
        {
            lblValidEmail.Text = txtEmailId.Text + ” is valid email”;
        }
        else
        {
            lblValidEmail.Text = txtEmailId.Text + ” is not valid email”;
        }
    }
    //This function returns true if the emailid matches the pattern. Else it will return false
    private bool IsValidEmailId(stringInputEmail)
    {
        //Regex To validate Email Address
        Regexregex = new Regex(@”^([w.-]+)@([w-]+)((.(w){2,3})+)$”);
        Matchmatch = regex.Match(InputEmail);
        if(match.Success)
            returntrue;
        else
            returnfalse;
    }
}
 Equivalent VB.Net Code;

ImportsSystem.Collections.Generic
ImportsSystem.Linq
ImportsSystem.Web
ImportsSystem.Web.UI
ImportsSystem.Web.UI.WebControls
ImportsSystem.Text.RegularExpressions
Partial Public Class ValidateEmail
    InheritsSystem.Web.UI.Page
    Protected Sub btnValidateEmail_Click(sender As Object, e As EventArgs)
        ‘Check Email Id is valid
        IfIsValidEmailId(txtEmailId.Text) Then
            lblValidEmail.Text = txtEmailId.Text + ” is valid email”
        Else
            lblValidEmail.Text = txtEmailId.Text + ” is not valid email”
        End If
    End Sub
    ‘This function returns true if the emailid matches the pattern. Else it will return false
    Private Function IsValidEmailId(InputEmail As String) As Boolean
        ‘Regex To validate Email Address
        Dimregex As New Regex(“^([w.-]+)@([w-]+)((.(w){2,3})+)$”)
        Dimmatch As Match= regex.Match(InputEmail)
        Ifmatch.Success Then
            ReturnTrue
        Else
            ReturnFalse
        End If
    End Function
End Class

Comments