aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Email Validation with Regular Expression

Aug 27, 2013

Here I will explain regular expression for email validation in c#, or how to validate email with regular expression in c#, or email address validation with regular expression in c#,


Regular Expression to Validate Email Address

To validate email address we need to write the regular expression like this

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 you want to see it in complete example write the following code in your aspx page like as shown below

<html xmlns="">
<title>Regular Expression to validate Email Address</title>
<form id="form1" runat="server">
<td><b>Enter Email:</b></td>
<asp:TextBox ID="txtEmail" runat="server" />
<td><asp:Button ID="btnValidate" runat="server" Text="Validate Email"
onclick="btnValidate_Click" /> </td>
<asp:label id="lblerrormsg" runat="server" style=" font-weight:bold" />
Once we write the code in aspx page add following namespaces in your code behind file

using System;
using System.Text.RegularExpressions;
Now write the following code in your code behind file

protected void btnValidate_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)
lblerrormsg.Text = "Enter Valid Email ID..";
lblerrormsg.ForeColor = System.Drawing.Color.Red;
lblerrormsg.Text = "Valid Email";
lblerrormsg.ForeColor = System.Drawing.Color.Green;


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Karan Reddy said...

Nice Program! Thanks

Saurabh said...
This comment has been removed by the author.
Anonymous said...

I don't understand the regular expression part. Plz explain in detail.Sorry i'm fresher in asp .net with c#. Thank you.

Anonymous said...

If I don't want to use a submit button what can I use?

surinder said...

It expression not used ( .com) key word.

Manish Kumar Gautam said...

How to validate an Email by Regular Expression?
string EmailPattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (Regex.IsMatch(Email, EmailPattern, RegexOptions.IgnoreCase))
Console.WriteLine("Email: {0} is valid.", Email);
Console.WriteLine("Email: {0} is not valid.", Email);

Use Reference String.Regex() Method

Unknown said...

If you don't want to use submit or any other button just use this code on text_change event....

Unknown said...

thanks it helped really

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.