Live username availability check while you type. When there is a pause of 500
milliseconds the username will be checked against the database.
The username choice will be reserved temporarily for 6 minutes, temporary blocking the username from other users.
If the username is not selected, it will be freed for others to reserve.
This example is using jQuery, PHP Data Objects and MySQL, of course you can use a different database if you wish.
| Username | ||
In this example I am using a delayCall function I have written, it can support multiple fields needed a delay:
function delayCall(obj,ms,callback){
return $(obj).each(function(){
if ( typeof this.timer == 'undefined'
){
// Define an
array to keep track of all fields/objects needed delays
// This is in
order to make this a multiple delay handling function
this.timer =
new Array();
}
var obj = this;
if (this.timer[obj.id]){
// in case
the timeout have not been reached and we get a new keystroke, we reset the timer
clearTimeout(this.timer[obj.id]);
delete(this.timer[obj.id]);
}
this.timer[obj.id] =
setTimeout(function(){
callback(obj);}, ms);
});
};
We use it this way basically:
$("#username").on("keyup",function(){delayCall($(this),500,fieldKeyup);});
You can download the complete example here.
What you need to have this sample on your site:
CREATE TABLE IF NOT EXISTS `username` (
`uname` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `username_temp` (
`uname` varchar(20) NOT NULL,
`sess_id` varchar(64) NOT NULL,
`timestamp` int(11) NOT NULL,
UNIQUE KEY `uname` (`uname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;