Username availability checker – Ajax Script

Username availability checker – Ajax Script

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
305 views

When you have a name as common as mine, you run across the entire gamut of schemes to deal with username availability in membership systems. By availability, of course I mean denial and rejection. Out of all of the ways that I’ve had “Dave” rejected, inline AJAX verification is definitely the least annoying. Wanting to be less-annoying myself, I’ve added the same functionality to my ASP.NET AJAX sites. Let me show you how I did it.

Note: I will preface this example by saying that an UpdatePanel is not the most efficient possible way to solve the problem. Using a web service or page method is much lighter over the wire. However, for the vast majority of sites, this method is perfectly suitable and much easier to work with.
Setting up the page.

For this example, I’m going to use a very simple registration form:

Username: <asp :TextBox runat="server" id="Username" ></asp><br />
Password: <asp :TextBox runat="server" ID="Password" ></asp><br />
Confirm: <asp :TextBox runat="server" ID="PasswordConfirm" ></asp><br />
<asp :Button runat="server" ID="Button1" Text="Sign me up!" ></asp>
Username: <asp :TextBox runat="server" id="Username" ></asp><br />
Password: <asp :TextBox runat="server" ID="Password" ></asp><br />
Confirm: <asp :TextBox runat="server" ID="PasswordConfirm" ></asp><br />
<asp :Button runat="server" ID="Button1" Text="Sign me up!" ></asp>

Since I want to work with it asynchronously, I’m going to wrap the Username TextBox in an UpdatePanel. I’m also going to enable AutoPostBacks on it and handle its OnTextChanged event. Inside an UpdatePanel, this will cause a partial postback to the event handler any time the TextBox loses focus and its contents have changed.

<asp :UpdatePanel runat="server" ID="up1">
  <contenttemplate>
    Username: <asp :TextBox runat="server" id="Username"
    AutoPostBack="true" OnTextChanged="Username_Changed" ></asp><br />
  </contenttemplate>
</asp>

Note that I only wrapped the username line in the UpdatePanel. If the entire form were in an UpdatePanel, the returning partial postback would revert all of the form fields to their values when the postback initiated. That behavior would be undesirable here, where users will likely be tabbing through the form quickly and might complete several other fields during the partial postback.
Checking username availability

I decided to check availability through a call to Membership.GetUser(). If a user with the supplied name exists, the method will return the corresponding MembershipUser. Otherwise, it returns null. With this in mind our OnTextChanged event handler is easy:

protected void Username_Changed(object sender, EventArgs e)
{
  if (Membership.GetUser(Username.Text) != null)
    // Display a username taken message.
  else
    // Display a username available message.
}

This method should work with any .NET Membership Provider setup, regardless of your data store or other customizations. Obviously, if you’re using a custom authentication system instead of the .NET Membership Provider, then you’ll need to perform the availability check a different way.

If anyone knows of a more efficient method for checking the username availability, I’d be interested in hearing it. Returning an entire MembershipUser object to express (in this case) what boils down to a boolean value is painfully wasteful.
Displaying the availability result

I chose to display my message in a div, just to the right of the username field:

<asp :UpdatePanel runat="server" ID="up1">
  <contenttemplate>
    Username: <asp :TextBox runat="server" id="Username"
    AutoPostBack="true" OnTextChanged="Username_Changed" ></asp>
    <div runat="server" id="UserAvailability"></div><br />
  </contenttemplate>
</asp>

With that element now accessible, I can complete the Username_Changed event handler:

protected void Username_Changed(object sender, EventArgs e)
{
  if (Membership.GetUser(Username.Text) != null)
  {
    UserAvailability.InnerText = "Username taken, sorry.";
    UserAvailability.Attributes.Add("class", "taken");
  }
  else
  {
    UserAvailability.InnerText = "Username available!";
    UserAvailability.Attributes.Add("class", "available");
  }
}

The relevant CSS:

#UserAvailability {
  padding-left: 22px;
  background-position: left;
  background-repeat: no-repeat;
}

.taken {
  background-image: url(taken.gif);
}

.available {
  background-image: url(available.gif);
}

The CSS classes, taken and available, are used to display a left positioned background image in the div (as seen in the screenshot above). The left padding ensures that the text message doesn’t overlap the status image. This visual cue is very powerful, and should not be overlooked.

Demo: http://ielliott.com/utest.php

Download: http://roshanbh.com.np/codes/username-available.zip


Source: http://ielliott.com/2007/06/ajax-username-availability-checker/

http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/delicious_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/blinklist_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/furl_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/technorati_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/magnolia_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/google_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/myspace_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/facebook_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/sphinn_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/mixx_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/twitter_32.png

Related Listings:

  1. Ajax Username Check – Using JQuery! The embedded javascript picks up the “onblur” event of the...
  2. Gmail Style, AJAX Check Username Signup I have made an example of the Gmail username style...
  3. ASP.NET AJAX File Downloads using IFRAMEs All implemented this functionality in ASP.NET applications at least several...
  4. Google like spell checker Script GoogieSpell is a spell checker that you can use in...
  5. Ajax Style File upload for .Net If you visit Asp.net Ajax Forum, you will find hundreds...

Do you like this post?

Email:     

Tags: , , , , , , , , , , , , , , , ,

2 Comments »

  1. avatar comment-top

    How come no one EVER does a complete sign up form. If I wanted to add this script to my current sign up form it wouldn’t work because of the other scripts used. When i hit submit it will submit the form without knowing if your script is valid or not. Can I get 1 complete sign up form from you geniuses.

    comment-bottom
  2. avatar comment-top

    If you know the javascript a bit you can easily implement this.

    BTW, what is the URL of your form where you are trying to implement ?

    so might can help you…

    comment-bottom

RSS feed for comments on this post. TrackBack URL

Leave a comment



Web Design & CSS (Templates) - TOP.ORG