Wednesday, 4 June 2014

Email Address Validation in Javascript in Asp.Net

6/04/2014 - By Pranav Singh 2

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.



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>
        &nbsp;<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";
        }
    }
}

VB.Net 
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.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top