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
{
//
}
}