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: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:
Comments
Post a Comment