Home > ASP.NET MVC | C# | jquery > Check username availability with jQuery, ASP.NET MVC, C#

Check username availability with jQuery, ASP.NET MVC, C#

This post shows how to add dynamic username availability lookup to an ASP.NET MVC view, using jQuery and C#. There are three parts to the solution: the html form, jQuery function, and the controller.

Html form

This part is pretty simple: a textbox for the username field with an onchange event, a div to hold the lookup results, and a lookup button.

    <h3>Username</h3>
    <%= Html.TextBox("Username", "", new { @onchange = "CheckAvailability()" })%>
    <div style="display:inline;" id="usernamelookupresult"></div><br/>
    <input type="button" value="Check Availability" onclick="CheckAvailability()" />
</code>

jQuery Username Lookup

This is where the magic happens. Well, nothing really magical, just a jQuery call to a controller, passing in the username value. In the example below, the function processing the result looks for a 0 to indicate the username is available. You can modify that to be whatever you want.


    function CheckAvailability() {
        $.post("/Home/CheckAvailability",
        { Username: $("#Username").val()},
            function(data) {
                var myObject = eval('(' + data + ')');
                var newid = myObject;
                if (newid == 0) {
                    $("#usernamelookupresult").html("<font color='green'>Available :-D</font>")
                }
                else {
                    $("#usernamelookupresult").html("<font color='red'>Taken :-(</font>")
                }
        });
    }

Controller

This is where your business logic lives, and is where the lookup occurs. For this exercise, we are keeping it simple, but you can see where to add your database lookup.


    public ActionResult CheckAvailability(string Username)
    {
            int Taken = 0;
            //  This is where you add your database lookup
            if (Username == "username")
            {
                Taken = 1;
            }
            return Json(Taken);
    }

Download Sample Application

Below is a quick clip to show the code from the downloadable sample in action:

,

Comments:2

Leave a Reply
  1. Michiel
    10/04/01

    OK, I have tried this but this always return 0 (available)???

  2. Michiel
    10/04/01

    “my bad!” just a typo! ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks:3

Listed below are links to weblogs that reference
Check username availability with jQuery, ASP.NET MVC, C# from C#, jQuery, .NET, MVC, nHibernate, MySQL
pingback from ASP.NET MVC Archived Blog Posts, Page 1 February 27, 2010

[...] to VoteCheck username availability with jQuery, ASP.NET MVC, C# (2/22/2010)Monday, February 22, 2010 from bill sternbergerThis post shows how to add dynamic username [...]

pingback from Dew Drop – March 25, 2010 | Alvin Ashcraft's Morning Dew March 25, 2010

[...] Check Username Availability with jQuery, ASP.NET MVC, C# (Bill Sternberger) [...]

trackback from uberVU - social comments March 27, 2010

Social comments and analytics for this post…

This post was mentioned on Twitter by LKWave: Check username availability with jQuery, ASP.NET MVC, C# http://bit.ly/b0cYg1 #MVC…

TOP