Hi power,
I have created sample code which full-fill your requirement. So please refer the below code and modify as per your requirement.
HTML
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtUsername" onChange="Username_OnChange(this)" onkeyup="keyupfunction(this)"
runat="server" />
<span id="Username_Check"></span>
</div>
<div>
<script type="text/javascript">
function UsernameAvailability(name) {
PageMethods.CheckUserName(name, Username_OnSuccess);
}
function Username_OnSuccess(response) {
var mesg = document.getElementById("Username_Check");
switch (response) {
case "true":
mesg.style.color = "";
mesg.innerHTML = "";
break;
case "false":
mesg.style.color = "red";
mesg.innerHTML = "Username already taken.";
break;
}
}
function Username_OnChange(txt) {
document.getElementById("Username_Check").innerHTML = "";
var mesg = document.getElementById("Username_Check");
mesg.innerHTML = "";
}
function keyupfunction(d) {
UsernameAvailability(d.value)
}
</script>
</div>
</form>
C#
[System.Web.Services.WebMethod()]
public static string CheckUserName(string userName)
{
string returnValue = string.Empty;
try
{
string consString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection conn = new SqlConnection(consString);
SqlCommand cmd = new SqlCommand("CheckUsername", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", userName.Trim());
conn.Open();
returnValue = cmd.ExecuteScalar().ToString();
conn.Close();
}
catch
{
returnValue = "error";
}
return returnValue;
}
Vb.Net
<System.Web.Services.WebMethod()> _
Public Shared Function CheckUserName(ByVal userName As String) As String
Dim returnValue As String = String.Empty
Try
Dim consString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim conn As New SqlConnection(consString)
Dim cmd As New SqlCommand("CheckUsername", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username", userName.Trim())
conn.Open()
returnValue = cmd.ExecuteScalar().ToString()
conn.Close()
Catch
returnValue = "error"
End Try
Return returnValue
End Function