CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
UserName or Login Availability Check using jQuery and Ajax in ASP.Net

By Bala Murugan
Posted On Oct 21, 2010
Article Rating:
Be first to rate
this article.
No of Comments: 2
Category: jQuery
Print this article.

UserName or Login Availability Check using jQuery in ASP.Net

It will be good and user-friendly if we provide a feature where users can know their preferred username is available to register in registration forms. For example, if you see Gmail registration form there will be button called “check availability!” which will help users to find if their desired username is available to pick. There is some handful of advantages of having this feature instead of informing the user on submit click about the username availability.

This will give a better user experience when the registration form is bigger and also will reduce the user’s effort to retype certain form fields like password, etc again and again because password fields will be reset in every submit.

Moving forward, we will see how to develop this feature in ASP.Net using Ajax methods in jQuery library.

Steps

1.      Open Visual Studio 2008, Create a new ASP.Net Website.

2.      Select a language of your choice in Language dropdownlist. Name your website as per your need.

3.      Download the latest jQuery library from jQuery.com and integrate it into your project. The following Faq’s will help you to that.

What is jQuery? How to use it in ASP.Net Pages?

How to enable jQuery intellisense in Visual Studio 2008?

How to use jQuery intellisense in an external javascript file?

4.      Include a new SQL express database inside App_Data folder and create a table called tblLogin with necessary columns.

Note

You can add database using the “Add New Item” dialog. Then, create a table called tblLogin with the necessary columns using the “Server Explorer”.

Refer the below figure,

UserName or UserID or LoginID Availability Check using jQuery and Ajax in ASP.Net

5.      Next, we will create an HttpHandler which will accept the desired username in querystring and check the tblLogin table for the availability. To create an HttpHandler, right click the project in your solution explorer and click “Add New Item”. Select “Generic Handler”. Rename it to LoginHandler.ashx and click OK. Refer the below code,

<%@ WebHandler Language="C#" Class="LoginHandler" %>

using System;

usingSystem.Web;

usingSystem.Data.SqlClient;

usingSystem.Configuration;

public class LoginHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string uname = context.Request["uname"];    

string result = "0";

if (uname != null)

        {

result = CheckUNAvailable(uname);           

        }

context.Response.Write(result);

context.Response.End();

    }

public string CheckUNAvailable(string name)

    {

string result = "0";

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);

try

        {           

con.Open();

SqlCommand com = new SqlCommand("Select Login from tblLogin where Login=@Login", con);

com.Parameters.AddWithValue("@Login", name);

objectuname = com.ExecuteScalar();

if (uname != null)

result = "1";

        }

catch

        {

result = "error";

        }

finally

        {

con.Close();

        }

return result;

    }

publicboolIsReusable {

get {

return false;

        }

    }

}

Web.Config

<connectionStrings>

              <add name="Sql" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;"/>

</connectionStrings>

6.      Now, we will consume this HttpHandler using jquery in Default.aspx and will provide appropriate message to let users know about the username availability. 




Default.aspx

<script src="_scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">

    $(function() {

        $("#btnAvailability").click(function() {

        $.post("LoginHandler.ashx", { uname: $("#<% =txtLogin.ClientID %>").val() }, function(result) {

if (result == "1") {                   

                    $("#dvMsg").html("Name already taken!");

                }

else if (result == "0") {                   

                    $("#dvMsg").html("Available");

                }

else {

                    $("#dvMsg").html("Error!");                   

          }

            });

        });

 

        $("#btnAvailability").ajaxError(function(event, request, settings, error) {

alert("Error requesting page " + settings.url + " Error:" + error);

        });

    });

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="txtLogin" runat="server"></asp:TextBox>

<input type="button" id="btnAvailability" value="Check Availability" />

<div id="dvMsg"></div>

7.      That’s it. Execute the application and see it in action.

OUTPUT

UserName or UserID or LoginID Availability Check using jQuery and Ajax in ASP.Net

UserName or UserID or LoginID Availability Check using jQuery and Ajax in ASP.Net

Downloads

Downloads Source

Conclusion

The registration form is one of first used feature in any public website and implementing this feature will give a good user experience. The introduction of jQuery has made client side scripting easy and simple. You can also implement this feature using ASP.Net AJAX but it is not that efficient when compared to jQuery Ajax methods due to the way it is processing the Ajax calls.

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Oops
Spot the error...

objectuname = com.ExecuteScalar();
if (uname != null)

jQuery username checking
It is very nice atical for check availability of username.Nice coding