ajax username availability

How to check username availability in php jquery ?.You can check username availability using jquery ajax post request.You can also use ajax start,ajax stop and other ajax function.When ajax start you can show loading and when ajax request complete you can hide it.You can free to use this script and modify also.

Jquery username check function

<script type="text/javascript">
     $(document).ready(function() {

            $("#btnChkUsername").click(function() {
                $("#succMsg").hide();
                $("#errMsg").hide();
               
                 $('#ajaxLoading') 
                .hide()  
                .ajaxStart(function() { 
                    $(this).show();
                  })
                  .ajaxStop(function() {  
                    $(this).hide();
                  })
                  .ajaxError(function(a, b, e) {
                        throw e;
                  });
           
                if($.trim($("#username").val())!=""){
                 
                     var usernameStr ='username='+ $.trim($("#username").val());
                     $.ajax({
                      url: "includes/checkUsername.php",
                      data:usernameStr,
                      type: "POST",
                      success: function(msg){
                          if($.trim(msg)=="succ"){
                            $("#succMsg").html('Congratulation! Username is available.');
                            $("#succMsg").show();
                          }
                          else if($.trim(msg)=="fail"){
                              $("#errMsg").html('This username is already taken.');  
                              $("#errMsg").show();
                          }
                          else if($.trim(msg)=="checkusername"){
                            $("#errMsg").html('Please enter valid username.');  
                            $("#errMsg").show();
                              
                          }
                          else{
                              $("#errMsg").html(msg);  
                              $("#errMsg").show();
                            
                          }   
                          
                        }
                    
                     });  
                    
                }
                else{
                     $("#errMsg").html('Please enter username.');  
                     $("#errMsg").show();
                     
                }
     
            }); 
     });  

    
   
   </script>  

Leave a Reply

Your email address will not be published. Required fields are marked *