jQuery AJAX username availability lookup

by Tom on April 5, 2008

One of the key design goals of our new Simple Web Management platform is ease of use. We want to deliver a joyous user experience.

If you read my post on password generation using twitter you will know I’m currently working on the process of adding users to the system. A neat little user experience trick I like, that you’ve no doubt seen elsewhere, is to check the availability of a username as the user is typing.

Availability

Looking up the username on the server and returning a response via AJAX is pretty simple (especially with JQuery), but every time the function is called a lookup on the user table is processed. Therefore, we don’t really want to do a lookup every time the user presses a key.

The first solution to the problem of this could be to add a ‘check availability’ link next to the username field, but this is a bit… erm… scrappy. The best option would be to wait until the user has stopped typing for a while. Well, thanks to a great little jQuery plugin called TypeWatch this is a doddle.

Here is my code. You’ll need jQuery and the TypeWatch plugin to make this work. Assumptions – my username field is ID’d Username, the AJAX query returns the text of the availability and puts it in to a span ID’d usernameAvailability.

//On page load.
$(document).ready(function() {

//Typewatch options
var typeWatchOptions = {
callback:function(){ checkUsernameAvailability(); },
wait:750
}

//Watch for when typing is finished in the username field.
$(“#Username”).typeWatch(typeWatchOptions);

//When a user is typing in the username field, we don’t want the availability status to show.
$(“#Username”).keypress(function() {
$(‘#usernameAvailability’).text(”);
});

});

//does the AJAX username lookup
function checkUsernameAvailability() {

$(‘#usernameAvailability’).text(‘Checking availability…’);

//Do an AJAX request to see if the username is available.
$.get(‘/action/people/is-username-available’, { username: $(‘#Username’).val() }, function(data) {
$(‘#usernameAvailability’).text(data);
});

}

{ 5 comments… read them below or add one }

Anthony Kosednar September 6, 2008 at 5:56 pm

Hey is there any chance that you could release the script for the random twitter password too? I would love to use that.. with credit of course.

Tom September 6, 2008 at 8:40 pm

Hey Anthony

Glad you like it.

I’ve got quite a lot of dev on my plate at the moment with some of the SWIM stuff (http://www.simpleweb-online.com/) we’re up to. I’m working tomorrow though so if I get a spare moment I’ll dig it out. Drop me an email to taholder[at]gmail[dot]com and I will forward it on – it’s in PHP

chrisdean November 13, 2009 at 12:30 pm

Hey,

Thanks for this!

It's literally what I'm just writing, to the letter, similar url structure, same id names as well – spooky!

I've just done a version which checks on every keyup which obviously isn't great requests wise, so this addition using typeWatch is just what I needed.

Chris

hugh87 December 3, 2009 at 3:01 pm

Hey cool code!!! It only lasts a little thing to be perfect:
The js fails when you write a username you erase it and then you retype exacly the same username. (the function dosn't check)

But so usefull!!!
thanks!!

hugh87 December 3, 2009 at 11:01 pm

Hey cool code!!! It only lasts a little thing to be perfect:
The js fails when you write a username you erase it and then you retype exacly the same username. (the function dosn't check)

But so usefull!!!
thanks!!

Leave a Comment

{ 1 trackback }

Previous post:

Next post: