Check user name login name email address availability using jquery ajax in Asp.Net page

In most registration page we need to check the availability of user name, email address using Jquery Ajax. Because it will be hectic job for user to rewrite the data after getting post back error that user name already taken or email address already taken. So it would be very helpful for user if he can check the name or login name or email address availability in time. To do that in this article i will show you GMAIL like feature by using Jquery & Asp.Net Ajax. The test output screen look like this:

Check user name login name email address availability using jquery ajax

To do the Example First Create a Table Like Below:

CREATE TABLE [dbo].[tblUsers](
	[ID] [int] NOT NULL,
	[Name] [varchar](max) NULL,
	[Email] [varchar](max) NULL,

Enter one Sample Data:

INSERT INTO tblUsers VALUES(1,'Shawpnendu','')

Modify the Web.config File To Connect to Database:

    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />

Note: Modify the connection string properties as per your setup.

Now Database part is completed. Concentrate on server side coding as well as HTML markup.

Now add the below HTML in your page:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <input type="button" id="cmdCheck" value="Available?" />
    <div id="dv_Ajax_Response" style="color:Red;font-weight:bold"></div>

Note: Style your CSS as you like.

Now add the JQuery Ajax Method:

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

<script type="text/javascript">
    $(function () {
        $("#cmdCheck").click(function () {
            $.post("user_Login.ashx", { user_name: $("#<% =txtName.ClientID %>").val() }, function (output) {
                if (output == "1") {
                    $("#dv_Ajax_Response").html("Name already exist!");
                else {
                    $("#dv_Ajax_Response").html("Still available");



Now almost done. This part a bit complex. Doing it carefully. Now add a handler by right clicking on solution name. Then click on Add New Item and then select Generic Handler. Name the handler to “user_Login.ashx”.

Write the below code within the handler:
C# Code:

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

using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

public class user_Login : IHttpHandler

    public void ProcessRequest(HttpContext context)
        string user_name = context.Request["user_name"];
        string output = "";
        output = CheckAvailability(user_name);

    public string CheckAvailability(string user_name)
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        SqlCommand com = new SqlCommand("Select Name from tblUsers where Name=@Name", con);
        com.Parameters.AddWithValue("@Name", user_name);
        string uName = (string)com.ExecuteScalar();
        if (uName != null)
            return "1";
            return "0";

    public bool IsReusable
        get { return false; }

Note: IsReusable read only property is required while implementing IHttpHandler. So don’t miss it.

VB.Net Code:

<%@ WebHandler Language="VB" Class="user_Login" %>

Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Configuration

Public Class user_Login : Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim user_name As String = context.Request("user_name")
        Dim output As String = ""
        output = CheckAvailability(user_name)
    End Sub
    Public Function CheckAvailability(user_name As String) As String
        Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
        Dim com As SqlCommand = New SqlCommand("Select Name from tblUsers where Name=@Name", con)
        com.Parameters.AddWithValue("@Name", user_name)
        Dim uName As String = CType(com.ExecuteScalar(), String)
        If uName <> Nothing Then
            Return "1"
            Return "0"
        End If
    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Return True
        End Get
    End Property
End Class

Note: IsReusable read only property is required while implementing IHttpHandler. So don’t miss it.

Now run the page. Hope you can now check the availability of user name or user login or email address before submitting the full registration page.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Ajax,, C#, Client, JQuery, VB.Net
One comment on “Check user name login name email address availability using jquery ajax in Asp.Net page
  1. Valentin says:

    Right here is the perfect website for everyone who wants to understand this topic. You understand a whole lot its almost hard to argue with you (not that I personally would want how to link google plus and facebook…HaHa). You certainly put a brand new spin on a subject that has been discussed for many years. Great stuff, just excellent!

Leave a Reply

Your email address will not be published. Required fields are marked *