ASP.NET - check duplicate value for username by javascript

Asked By myzonal.com myzonal.com on 05-Sep-09 04:31 AM
i m entering username so we want to check user not enter same username,
so we want to check username by javascript code in clientside.
plz help me as soon.
thanks and regards
viek sachan
 

try ICallback or AJAX

sharath chandran replied to myzonal.com myzonal.com on 05-Sep-09 04:54 AM
you can try using ICallback which is a light weight component or can try AJAX, AJAX being simple to implement compared to ICallback and .Net AJAX controls internally uses Javascript to do the job, but to do something which is quite transparent and that provide good control over code, try your own using ICallback, try referencing this URL for brief info on ICallback if u are new to it.
http://www.codeproject.com/KB/aspnet/ICallbackJSON.aspx

check duplicate value for username by javascript

mv ark replied to myzonal.com myzonal.com on 05-Sep-09 05:22 AM
You can use XMLHTTPRequest Object in JavaScript to asynchronously check if a user name already exists in the database. This article has a detailed code sample to illustrate how user name validation can be done on the client-side - http://www.15seconds.com/Issue/020606.htm

It references a server-side page to check if similar user name already exists in the database. I suggest that you implement a generic handler (.ashx) for the server side validation as it is comparatively faster than using a .aspx page

Where do you store the usernames?

[)ia6l0 iii replied to myzonal.com myzonal.com on 05-Sep-09 07:07 AM
You cannot check for the duplicate username by javascript alone, as you would never have the store for the usernames on the client side. 

Does that make things clear?

You would need to go the server, access your database layer, go to database, execut an stored procedure on your database to figure out if an user already exists with the same username (case-sensitive?) and come back to the calling page (asp.net) and alert the user.

If  you are storing the usernames in an ArrayList or any collection like below. Then you could do a simple check if the username exists in it and send back the result. 

ArrayList alUserNames = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
alUserNames.AddRange(new string[] { "John", "McSilj", "Greg","Mike"};
 }

protected void btnCheckForUser_Click(object sender, ButtonClickArgs e)
{
            if (alUserNames.Contains(txtUserName.Text)            {
                lblResult.Text = "User name exists";            }
}

If you are going to hit a database, i would prefer to do it the Ajax way. 

Put your username textbox in a UpdatePanel. The check can be done in the OnTextChanged Event handler of the username textbox.  Add a div to show the result.

This would be the UpdatePanel Markup.
<asp:UpdatePanel ID="upUserName" runat="server">
  <ContentTemplate>
    Enter your Username: <asp:TextBox ID="txtUsername"  runat="server"  AutoPostBack="true" OnTextChanged="txtUsername_Changed" />
<div runat="server" id="divResult"></div>
  </ContentTemplate>
</asp:UpdatePanel>

Note: Autopostback on the textbox is necessary.

And in your event handler, all you have to do is populate the div with the result after you perform a check.

protected void txtUsername_Changed(object sender, EventArgs e)
{
  if (usernamealreadyexists(txtUsername.Text))
  {
    divResult.InnerText = "already exists.";
  }
  else
  {
   //
  }
}