Monday, March 21, 2011

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 or 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 his name or login name or email address availability in time without losing any data. 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 login Name Availability

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:
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);
        context.Response.Write(output);
        context.Response.End();
    }

    public string CheckAvailability(string user_name)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand("Select User_Name from tblUsers where User_Name=@Name", con);
        com.Parameters.AddWithValue("@Name", user_name);
        string uName = (string)com.ExecuteScalar();
        con.Close();
        if (uName != null)
            return "1";
        else
            return "0";
    }
    
    public bool IsReusable 
    {
        get {return false;}
    }
}

Now add an aspx page & write the below HTML code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Check_USER_EMAIL.aspx.cs" Inherits="Check_USER_EMAIL" %>

<!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>Check user or email address availability</title>
    <script src="Script/jquery.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");
            }

        });
        });
    });

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <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>
    </div>
    </form>
</body>
</html>

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

3 comments:

autumcloud said...

Thank you so much . All of your post are valuable for me and help me a lot . May I share these article ?

aayyaa said...

could you do another post on how to check user availability in windows based application using vb.net?
I really need some ideas on it.
tHnks

Ranjan said...

Thank u so much, but I want to check both username and email id using java script can u tel how to do or any demo help me pls

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash