Tuesday, September 22, 2009

Check User availablity through Jquery and ajax

First Step : Create page for check user name

$(document).ready(function() {
$("#txtUserName").blur(function(e) {
if ($("#txtUserName").val().length > 5) {

setTimeout(function() { onChange(); }, 500);
}
else {
$("#img").fadeOut('normal');
$("#lblMsg").remove();
$("#imgin").remove();
$("#txtUserName").focus();
$("#img").append('Enter Minimum 6 Character');
$("#img").fadeIn('slow');

}


});

$("# txtUserName ").keypress(function(e) {
if ($("#txtUserName ").val().length > 5) {

// track last key pressed
lastKeyPressCode = e.keyCode;
switch (e.keyCode) {
case 38: // up
e.preventDefault();
moveSelect(-1);
break;
case 40: // down
e.preventDefault();
moveSelect(1);
break;
case 9: // tab
case 13: // return
if (selectCurrent()) {
// make sure to blur off the current field
$input.get(0).blur();
e.preventDefault();
}
break;
default:
setTimeout(function() { onChange(); }, 500);
break;
}
}
});
function onChange() {

$.ajax({
type: "POST",
url: "webform1.aspx",
data: "val=" + $("# txtUserName ").val(),
success: function(msg) {
if ($(msg).find("#lbl").html() == 'Available') {
//$("#lbl").text($(msg).find("#lbl").html());

$("#img").fadeOut('normal');
$("#imgin").remove();
$("#lblMsg").remove();
$("#img").append('');
$("#img").fadeIn('slow');
}
else {
$("#img").fadeOut('normal');
$("#imgin").remove();
$("#lblMsg").remove();
$("#txtUserName").focus();
$("#img").append('');
$("#img").fadeIn('slow');
}
}
});
}


});

2nd step : to access data from database
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["val"]!=null)
{
SqlConnection connectrion =new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
connectrion.Open();
string cmdText = "select 1 from userlogin(nolock) where username='" + Request.Form["val"].ToString()+"'";
SqlCommand com = new SqlCommand(cmdText);
com.Connection = connectrion;
string result;
result = Convert.ToString(com.ExecuteScalar());
connectrion.Close();
if (result =="1")
lbl.Text = "Available";
else
lbl.Text = "Not Available";
}
else
{
lbl.Text = "false";
}
}

No comments:

Post a Comment