Username Availability Validator almost ready, the early demo

by mosessaur| 19 September 2008| 15 Comments

UPDATE: ALWAYS DOWNLOAD LATEST SOURCE CODE FROM PROJECT PAGE ON CODEPLEX

Few weeks ago Dave started the Advanced ASP.NET AJAX Server Controls book giveaway contest. And yesterday he announced that the control is almost completed, but the contest door is still open. So I thought to grape the bits and start testing it and build a simple sample. [View Demo]

The sample that I am going to demonstrate here will use ASP.NET Create User Wizard control with UserName Availability Validator. In this sample I'll show the important properties of this control as well as the current issues exist.

Prerequisites:

I'll assume that you already know how to configure and use the existing built in ASP.NET Membership provider. You'll need a sample database with ASP.NET Membership services installed on it.

The sample provided has ASP.NET Membership already installed and configured, you can download the sample and start explore it.

You page must have ScriptManager control on it, of course this is an ASP.NET AJAX control at the end.

Using CreateUserWizard control:

In order to be able to use the UserName Availability Validator control with CreateUserWizard ASP.NET Control, you need to customize create user step. To do that you can follow these steps:

  1. Add CreateUserWizard control to your page.
    createuserwizard00
  2. From the Smart Tag menu select Customize Create User Step.
    createuserwizard01
  3. Drag and drop Username Availability Control From toolbox just next UserName TextBox. And set ControlToValidate property to UserName TextBox.
    createuserwizard02

For step 3, you can do that through ASPX code all you need to do is to register the control on the page:

<%@ Register Assembly="UAV" Namespace="Encosia" TagPrefix="cc1" %>
Then add the control near UserName TextBox:
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
	ControlToValidate="UserName" ErrorMessage="User Name is required." 
	ToolTip="User Name is required."
	ValidationGroup="CreateUserWizard" Text="*"/>
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1" 
	 runat="server" ControlToValidate="UserName" 
	ErrorMessage="Username is not unique" />
The control starts at line 6.

UserNameAvailabilityValidator Properties:

This control inherits from BaseValidator control. So basically it inherits all its properties. Here I'll focus on the new functional properties provided by the exclusively by the control:

  • ServicePath: Path of the web service that is responsible to check for UserName availability. Checkout this thread post.
  • ServiceMethod: Name of the web method in the web service specified in ServicePath that is responsible for performing the logic to check for UserName availability. It is called when the validation process starts. This method must accept one parameter string called "username" and returns a boolean value. Checkout this thread post.
  • MinimumLength: Gets or sets a value indicating minimum number of characters in UserName before validation process take place.
  • ValidateOnKeyPress: A boolean value that indicates whether the validation process should take place upon keypress or not.
  • KeyPressDelay: Only has effect with ValidateOnKeyPress is set to true. A value in milliseconds to indicate how long after keyup validation happens.

Membership Service:

In order to complete this, a web service is required to check for username availability in data source. And as I'm using ASP.NET Membership Services, this service is very simple. I just need one method as the following:

[WebMethod]
public bool IsUsernameAvailable(string username)
{
   return System.Web.Security.Membership.GetUser(username) == null;        
}
And here here is the full Control Declaration:
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1" 
	runat="server" 
	ControlToValidate="UserName" 
	ErrorMessage="Username is not unique" 
	KeyPressDelay="500" 
	MinimumLength="5" 
	ServiceMethod="IsUsernameAvailable" 
	ServicePath="~/MembershipService.asmx" 
	ValidateOnKeyPress="True"/>
It worth to mention that you can use Page Method as well. Just declare one as the following in your User Creation Page:
[System.Web.Services.WebMethod]
public static bool IsUsernameAvailable(string username)
{
   return System.Web.Security.Membership.GetUser(username) == null;
}
Then modify Control ASPX code -Line 8- for the ServicePath to point to your page:
<cc1:UsernameAvailabilityValidator ID="UsernameAvailabilityValidator1" 
	runat="server" 
	ControlToValidate="UserName" 
	ErrorMessage="Username is not unique" 
	KeyPressDelay="500" 
	MinimumLength="5" 
	ServiceMethod="IsUsernameAvailable" 
	ServicePath="~/CreateUser.aspx" 
	ValidateOnKeyPress="True"/>
Known Issues:

The control isn't in release yet, this is just an early demo to show how to use it. So there is no wonder if you faced some issues.

The issues that I found are as the following:

  • When ValidateOnKeyPress is set to true, any key press will trigger the validation process. Which on the other hand will initiate AJAX request.
  • When ValidateOnKeyPress is set to true and KeyPressDelay is set to a small value. And you typed your username very quickly, multiple validation requests will be triggered with multiple AJAX calls to the service. This will cause an error message box to be displayed frequently.
  • There is an issue that appears with FF, Opera, Safari and Chrome, not a high rank issue. Immediately after finishing typing your username and just upon validation process starts the error message flicker for few milliseconds and then disappears till the validation completes. I didn't notice this issue with IE7.

Conclusion:

I liked the idea of having a contest around free give away book, it actually brings out the good of being in a community and participate even if you might not win. You'll see different developers thinking together and exchange ideas to build something and resolve its issues.

If you want to participate, return to Dave's posts and visit the project page on CodePlex. I really hope that this control continue developing and enhance its features. It is simple but cool and handy.

You can download this sample and view the demo here.

kick it on DotNetKicks.com

Comments

trackback
DotNetKicks.com on 9/15/2008 11:44 AM Trackback from DotNetKicks.com

Username Availability Validator almost ready, the early demo
Amre Ellafi
Amre Ellafi Egypt on 9/21/2008 12:15 AM Well and professionally done ! i will call you to borrow the book when you win it Smile
mosessaur
mosessaur Egypt on 9/21/2008 2:41 AM @Amr Ellafi LOL, I don't expect that much to win. I just wanted to have a finger print on it.
pingback
yeschandana.com on 9/22/2008 9:43 AM Pingback from yeschandana.com

My favorite links from the 4th week of September 2008
pingback
encosia.com on 9/26/2008 11:14 AM Pingback from encosia.com

Username Availability Validator v1.0 released | Encosia
pingback
zdima.net on 9/27/2008 10:19 AM Pingback from zdima.net

Username Availability Validator v1.0 released @ ZDima.net
hashad
hashad Egypt on 10/30/2008 8:31 AM thnx man for your demo
but how can i use image if user is Availabil or not
like this
encosia.com/.../
mosessaur
mosessaur Egypt on 10/30/2008 7:53 PM @hashad First of all you need to download the release 1 of this control at http://www.codeplex.com/UsernameAvailability
Then to do that I think you need to play with CSS and set background image for success or error message.
I'll also look at it and try to get back with some demo if I got some time.
quachnguyen
quachnguyen Vietnam on 12/17/2008 3:37 AM When i type asdf on username field, then I tab to another filed, Availability Validator says this Username is taken. It disappear on 3s

Regards,
Neon

nadia
nadia India on 12/27/2008 3:15 PM check http://www.dialusername.com/
Gurjeet Saini
Gurjeet Saini United States on 3/3/2009 9:06 AM Hello,
I have tried and works fine except to clash with other functionality i.e. dropdown seelect index change functionality and text box mask etc.


Thanks
Gurjeet Saini
amjad
amjad Jordan on 4/6/2009 9:29 PM mohammed please how can i get "UserNameAvailabilityValidator" mentioned in step 3
martin
martin United States on 5/17/2009 1:42 AM Hi - Great control just cat get it to work!!

OK so i downloaded the latest version from Moses web-blog
-I dropped the dll in my bin folder and added a ref?
-I added the dll to my toolbox
-Then I added the control to my page - (i have a user wizard)

<%@ Register Assembly="UsernameAvailabilityValidator" Namespace="Encosia" TagPrefix="cc2" %>

<cc2:UsernameAvailabilityValidator
                                        ID="UsernameAvailabilityValidator1"
                                        runat="server"
                                            ErrorMessage="Taken"
                                            SuccessMessage="Available."
                                             ControlToValidate="UserName"
                                            ValidationGroup="CreateUserWizard1"
                                            CssClass="taken"
                                            SuccessCssClass="available"
                                            ServiceMethod="IsUsernameAvailable"
                                            ServicePath="MembershipService.asmx"
                                            ValidateOnKeyPress="True"
                                            MinimumLength="8" Display="Dynamic"

                                            >
                                          
                                            </cc2:UsernameAvailabilityValidator>


And I run....

I Enter a user name and viola! it works a treat - but when i click my create user button i get an exception -

so i dropped your default page in my app and i get the same error

but if i load your demo it works.

here is the error


Server Error in '/Todolist' Application.
--------------------------------------------------------------------------------

The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="%2fTodolist%2fLogin%2fMembershipService.asmx%3fAspxAutoDetectCookieSupport%3d1">here</a>.</h2>
</body></html>

--.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="%2fTodolist%2fLogin%2fMembershipService.asmx%3fAspxAutoDetectCookieSupport%3d1">here</a>.</h2>
</body></html>

--.

Source Error:


Line 173:                    object webservice = result.CompiledAssembly.CreateInstance(types[0].Name);
Line 174:                    MethodInfo mi = webservice.GetType().GetMethod(ServiceMethod);
Line 175:                    retVal = (bool)(mi.Invoke(webservice, new object[] { controlValidationValue }));
Line 176:                }
Line 177:            }

mosessaur
mosessaur Egypt on 5/17/2009 8:31 AM You will need to have the “MembershipService.asmx” or create your own to match your business needs.
Checkout the simple documentation at usernameavailability.codeplex.com/.../View.aspx
club penguin
club penguin United States on 5/18/2009 1:47 PM I have tried and works fine except to clash with other functionality i.e. dropdown seelect index change functionality and text box mask etc.
Comments are closed