Sunday, 2 June 2013

Username availability check using PHP and jQuery



Username availability check using PHP and jQuery


In this tutorial we provide  simple code to check username availability checking using php and jquery.

This tutorial contain two files :
        1- index.html
        2-checkuser.php

index.html :

code :

<head>
<title>Username availability check using PHP and jQuery</title>

<style>
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;
}
</style>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
<!-- http://www.itechroom.com-->
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>
</head>
<body>
<div  style="width:800px; margin:0 auto;">
<div style=" padding:20px 0px;"><h2>Username availability check using PHP and jQuery</h2></div>

<div style="padding:100px 0px  10px 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>
<div style="padding:10px 0px  50px 10px;">
Password : <input type="password" name="pwd" />
</div>
<div style="padding:0px 0px  50px 10px;"> For testing, registered username are: rex108,www108,rajsingh </div>
<div align="center"><h3>Visit <a href="http://wwww.phpedu108.blogspot" target="_blank"> www.phpedu108.blogspot.in</a> for more.</h3></div>
</div>
</body>

</html>




checkuser.php :

code :
<?php 
// http://www.phpedu108.blogspot.For more tutorial visit

$arr_user=array("rex108", "www108","rajsingh");
$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>';
}

?>




No comments:

Post a Comment