Today I am was developing a registration form. So while
developing the registration form I come across a problem for validating the
email id entered by user.
So I found a solution to validate the email id using JavaScriptI our asp.net registration form. In this article I have not used regular expression
for asp.net validation control.
In my previous article I have shown you Email
Validation in Windows Application C#.Net and VB.Net | Validating Email ID in
TextBox in C# .Net.
Some of the other asp.net application are as follows: Free
jQuery Spell Checker Plugins For Asp.Net, Passing
Value From One Form to Another in Asp.net Using C#.Net, How
to style input and submit button with CSS in Asp.net, Binding
Gridview By Access DataBase Using C#.Net in Asp.Net.
For this first we will create new asp.net application and
add the below code in your asp.net page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmailValidation.aspx.cs"
Inherits="ProjectDemo_Asp.et.EmailValidation" %>
<!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 runat="server">
<title>Email
Validation Using javascript In Asp.Net</title>
<script language="javascript" type="text/javascript">
function
ValidateEmailId() {
var
value = document.getElementById("<%=txtemailid.ClientID
%>").value;
var
atposition = value.indexOf("@");
var
dotposition = value.lastIndexOf(".");
if
(atposition < 1 || dotposition < atposition + 2 || dotposition + 2
>= value.length) {
alert("Please enter a valid e-mail address");
return false;
}
else
{
return
true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtemailid" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="Submit"
onclick="Button1_Click" OnClientClick="javascript:return ValidateEmailId();"/>
<br />
<asp:Label ID="Label1" runat="server" Style="color: #FF0000"></asp:Label>
</div>
</form>
</body>
</html>
|
In above code check the javascript
<script language="javascript" type="text/javascript">
function
ValidateEmailId() {
var
value = document.getElementById("<%=txtemailid.ClientID
%>").value;
var
atposition = value.indexOf("@");
var
dotposition = value.lastIndexOf(".");
if
(atposition < 1 || dotposition < atposition + 2 || dotposition + 2
>= value.length) {
alert("Please enter a valid e-mail address");
return false;
}
else
{
return
true;
}
}
</script>
|
In above code first we are getting index of @ and after that
for dot(.) . These two are the most important
part of the of an email id. After proper validation we are making decision
weather user have entered valid email id or not. If no then error message is
getting displayed otherwise page post back take pace.
Now add the below code in you .as page for displaying the message.
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class EmailValidation : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
Label1.Text = "You have entered a valid e-mail address";
}
}
}
|
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ProjectDemo_Asp.et
Partial Public Class EmailValidation
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
End Sub
Protected
Sub Button1_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
Label1.Text = "You have entered a valid e-mail address"
End Sub
End Class
End Namespace
|
Now right click and view the page in browser.
nice
ReplyDeleteThanks
Delete