Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
on blank textbox Jquery username availability showing "Available" i'm cheking availability on keyup event.

my code is

C#
[System.Web.Services.WebMethod]
    public static string CheckUserName(string userName)
    {
        string returnValue = string.Empty;
        try
        {
            string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
            SqlConnection Sqlconn = new SqlConnection(SqlConnect); 
            SqlCommand cmd = new SqlCommand("spx_CheckUserAvailability",Sqlconn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", userName.Trim());
            Sqlconn.Open();
            returnValue = cmd.ExecuteScalar().ToString();
            Sqlconn.Close();
        }
        catch
        {
            returnValue = "error";
        }
        return returnValue;
    }


source part-
HTML
<script src="js/jquery-1.3.2.min.js"type="text/javascript"></script>


 
      <script type = "text/javascript">
     function ShowAvailability() {

    $.ajax({
        type: "POST",
        url: "Signup.aspx/CheckUserName",
        data: '{userName: "' + $("#<%=TextBox4.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response);
        }
    });
}

function OnSuccess(response) {
    var mesg = $("#mesg")[0];

    switch (response.d) {
        case "true":
            mesg.style.color = "green";
            mesg.innerHTML = "<img src='image/tick.png' width='13px' height='13px'> Available";
            
            break;
        case "false":
            mesg.style.color = "red";
            mesg.innerHTML = "<img src='image/unavailable.png' width='13px' height='13px'> Not Available";
            break;
        case "error":
            mesg.style.color = "red";
            mesg.innerHTML = "Error occured";
            break;
//        case  "null":
//            mesg.style.color="black";
//            mesg.innerHTML="Fill email ID";
//            break;                     
    }
}
function OnChange(txt) {
   $("#mesg")[0].innerHTML = "";
}
</script> 
Posted
Comments
Sandeep Mewara 20-Oct-12 13:40pm    
And the issue is?
Surendra0x2 20-Oct-12 13:42pm    
on Keyup if textbox is blank then it is showing available
Sergey Alexandrovich Kryukov 20-Oct-12 22:57pm    
Not a question.
--SA

1 solution

The right way to do this is to modify your javascript function
C#
function ShowAvailability()
to send ajax calls to server only in case the textbox is having some content.
JavaScript
function ShowAvailability() {
  if($.trim($('#'+'<%=txtUserName.ClientID%>').val())!="")//replace txtUserName with id of your textbox
  {
   //your code.
  }
}

Also you can modify your server side code to include null check.
C#
public static string CheckUserName(string userName)
        {
            string returnValue = string.Empty;
            if ( userName.Trim() != "")
            {
                try
                {
                    string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
                    SqlConnection Sqlconn = new SqlConnection(SqlConnect);
                    SqlCommand cmd = new SqlCommand("spx_CheckUserAvailability", Sqlconn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserName", userName.Trim());
                    Sqlconn.Open();
                    returnValue = cmd.ExecuteScalar().ToString();
                    Sqlconn.Close();
                }
                catch
                {
                    returnValue = "error";
                }
            }
            else
                returnValue = "null";
            return returnValue;
        }
 
Share this answer
 
Comments
Surendra0x2 22-Oct-12 15:05pm    
Thanks Sir...
But sir there is an another problem related to this on key up event when i'm deleting textbox text n making textbox blank then it shows Available
Surendra0x2 22-Oct-12 15:08pm    
here is the registration form sir plz can you check the problem.
http://surendrakumarverma.com/Signup.aspx
TheCoolCoder 24-Oct-12 22:43pm    
I am not able to view your page, as it is blocked by firewall in my system.. why are you deleting textbox text in keyup event?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900