Asp.net Email Address Validation Using Regular Expression

Email Address Validation for ASP.NET ( Validation Using Regular Expression):



In this post we learn how to use regular Expression in C#code for validation of Email id in asp.net web application. Some time we see many web sites use the Email validation in registration time, subscribe time etc. So how we can use Email validation in my asp.net web site.

Basically the JavaScript use for the validation in client site. But Asp.net also provide a validation class we can use this for check the valid information of not.


Asp.net Email Address Validation Using Regular Expression(parijat)


Asp:RegularExpressionValidator for Email address:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmailValidation_Csharp.aspx.cs"
    Inherits="EmailValidation_Csharp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Asp.net Regular Expression to validate Email Address</title>
    <style type="text/css">
        .style1
        {
            color: #990000;
            font-weight: bold;
        }
        .style2
        {
            color: #990000;
            text-decoration: underline;
            font-family: Arial, Helvetica, sans-serif;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <h2 class="style2">Check valid Email-ID by C# code</h2>
    <table style="width: 396px; height: 128px; background-color: #00CCFF">
        <tr>
            <td>
                <span class="style1">Enter Email:</span>
            </td>
            <td align="left">
                <asp:TextBox ID="txtEmail" runat="server" Width="195px" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td align="left" valign="top">
                <asp:Button ID="btncheck" runat="server" Text="Validate Email"
                    Height="31px"
                    style="color: #990000; font-weight: 700" onclick="btncheck_Click" />
                <br />
                <br />
    <asp:Label ID="lblmsg" runat="server" Style="font-weight: bold;" />
            </td>
        </tr>
    </table>
    <br />
    This Example in <a href="http://asp-net-by-parijat.blogspot.in/">Asp.net Tutorial
        </a>
        <br />
    </div>
    </form>
</body>
</html> 

Validate an email address using c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class EmailValidation_Csharp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btncheck_Click(object sender, EventArgs e)
    {

        bool isEmail = Regex.IsMatch(txtEmail.Text.Trim(), @"\A(?:[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)\Z");
        if (!isEmail)
        {
            lblmsg.Text = "Enter Valid Email ID..";
            lblmsg.ForeColor = System.Drawing.Color.Red;
            return;
        }
        else
        {
            lblmsg.Text = "Valid Email";
            lblmsg.ForeColor = System.Drawing.Color.Green;
        }
    }
}




Other ASP.NET Programming Post:  
    
·                     Using jQuery change Background color of DIV
·                     JQuery Change Div Background Color Randomly
·                     Get selected radio button values using JQuery.
·                     How do you do html text encodes using JavaScript
·                     Limit Number of Characters in a TextArea using jQuery
·                     Characters inTextbox  in asp.netusing jquery:
·                     Drag and Drop Sortable Lists using jQueryUI
·                     jquery tooltip with css in asp.net web page
·                     Ajax ColorPickerExtender in ASP.NET Example
·                     What is the Ajax colorpicker,How to use Ajax colorpicker
·                     JQuery UI Datepicker (Calendar) with asp.net textbox
·                     Adding or binding tooltip in each item of dropdownlist
·                     jquery disable or Enable submit button after validation
·                     Example of Adding ToolTip for each Dropdown List Item in C#
·                     Displaying the textbox value in javascript Messagebox
·                     Enable Disable Submit Button using jQuery
·                     jquery disable or Enable submit button after validation
·                     What is ASP.NET FRAMEWORK PART 1 Programming
·                     Display selected Date from data base into asp.net calendar
·                     How to create Line chart:,How to Make Data Table By C# code:
·                     Example of Crystal report(Crystal_report_in asp.net programming )
·                     SQL Helper ClassExample of how to add captcha in Asp.net using C#
·                     Sql Query for asp.net Programming How to create graph in asp.net:
·                     How to use CAPTCHA in asp.net,How to use Asp.net regular expression
·                     Show tooltip dropdownlist items by mouseoverevent using C# example  

Comments