Username availability check using PHP and jQuery

It’s always good to check the username or email for availability before submission. So the user can check it instantly and helps them to enter the valid one.

This can be done using PHP and jQuery. Just a little code will do the rest.

Script used:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
 
<script type="text/javascript">
function checkUserName(usercheck)
{
	$('#usercheck').html('<img src="images/ajax-loader.gif" />');
	$.post("checkuser.php", {user_name: usercheck} , function(data)
		{			
			   if (data != '' || data != undefined || data != null) 
			   {				   
				  $('#usercheck').html(data);	
			   }
          });
}
</script>

The CSS used:

body{
padding:0;
margin:0;
font-family:"Trebuchet Ms";
color:#2a2a2a;
font-size:14px;
line-height:18px;
}
 
.error{
color: #FF0000;
font-size:11px;
}
 
.success{
color: #33CC00;
font-size:11px;
}

The HTML page:

<div style="padding:100px 0px  50px 10px;">
Username : <input type="text" name="username" id="username" onblur="checkUserName(this.value)" /> 
<span id="usercheck" style="padding-left:10px; ; vertical-align: middle;"></span>
</div>

The PHP (processing) page:

<?php 
 
$arr_user=array("itechroom", "trialuser");
$username=$_POST['user_name'];
 
if(in_array($username,$arr_user))
{echo '<span class="error">Username already exists.</span>';exit;}
else if(strlen($username) < 6 || strlen($username) > 15){echo '<span class="error">Username must be 6 to 15 characters</span>';}
else if (preg_match("/^[a-zA-Z1-9]+$/", $username)) 
{
       echo '<span class="success">Username is available.</span>';
} 
else 
{
      echo '<span class="error">Use alphanumeric characters only.</span>';
}
?>

View Demo Download

Be Sociable, Share!
You can leave a response, or trackback from your own site.

7 Responses to “Username availability check using PHP and jQuery”

  1. [...] have seen checking username availability using jQuery in earlier post. Here is a slight modification on showing of error/success message. [...]

  2. [...] 4. בדיקת זמינות משתמש באמצעות PHP ו-jQuery [...]

  3. [...] Username availability check using PHP and jQuery [...]

  4. [...] 7) Username Availability Check Using PHP and jQuery [...]

  5. Maxim says:

    Please Modify the above code to prevent submission if username is not available.

Leave a Reply

CommentLuv badge
 

Powered by WordPress