Home > AJAX > Check UserName Availability with AJAX

Check UserName Availability with AJAX


try this example to check username availability with ajax

step1: Create Webservice called UsernameService.asmx

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Security;
/// <summary>
/// Summary description for UsernameService
/// </summary>
[WebService(Namespace = "<a href="http://tempuri.org/">http://tempuri.org/</a>")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class UsernameService : System.Web.Services.WebService {

    public UsernameService () {

        //Uncomment the following line if using designed components
    public string[] IsAvailable(string TocheckFor)
        string[] myString = new string[2];
        if (Membership.GetUser(TocheckFor) != null)
            myString[0] = string.Format("{0} is taken", TocheckFor);
            myString[1] = "false";
            myString[0] = string.Format("{0} is available", TocheckFor);
            myString[1] = "true";           
        return myString;

step2: Create Web page Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "<a href="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" >
    function checkName()
        var tb = new Sys.Preview.UI.TextBox($get('Username'));
        var name = tb.get_text();
        if(name.lenght != 0)
            UsernameService.IsAvailable(name, onIsAvailableCompleted)
    function onIsAvailableCompleted(result)
        if(result != null)
            var lbl = new Sys.Preview.UI.Label($get('lblAvailability'));
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <asp:ServiceReference Path="UsernameService.asmx" />
            <asp:ScriptReference Name="PreviewScript.js" Assembly="Microsoft.Web.Preview" />
            Username: <asp:TextBox ID="Username" runat="server" Width="93px"></asp:TextBox>
            <asp:Label ID="lblAvailability" runat="server" Text="Type a username" ForeColor="DimGray"></asp:Label>

in code behind :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        Username.Attributes.Add("onkeyup", "checkName()");

Hope it helps

Good Luck

  1. January 9, 2009 at 7:56 am
  2. January 16, 2009 at 10:08 pm

    Interesting, but usual =)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: