Reader Level:

Client Side Username Availability Checking in MVC

By Abhimanyu K Vatsa on Mar 15, 2013
It is often required live ‘username’ checking on user registration page in web applications. Today I developed this for my one of the web application and would like to share that with you.

It is often necessary to check a "username" in a user registration page live in web applications. Today I developed this for one of my web applications and would like to share it with you.

Let's look at the following GIF screen before looking at the code.

Now, to develop this you just need to make an Ajax GET call to a method in the "Account" controller.


Here is the Register.cshtml code, I highlighted the changes.


@model MvcApplication1.Models.RegisterModel


    ViewBag.Title = "Register";


<hgroup class="title">


    <h2>Create a new account.</h2>


@using (Html.BeginForm()) {




        <legend>Registration Form</legend>



                @Html.LabelFor(m => m.UserName)

                @Html.TextBoxFor(m => m.UserName)

                <span id="result" />



                @Html.LabelFor(m => m.Password)

                @Html.PasswordFor(m => m.Password)



                @Html.LabelFor(m => m.ConfirmPassword)

                @Html.PasswordFor(m => m.ConfirmPassword)



        <input type="submit" value="Register" />



@section Scripts {


    <script type="text/jscript">

        $('#UserName').blur(function () {

            var url = "/Account/CheckUserName";

            var name = $('#UserName').val();

            $.get(url, { input: name }, function (data) {

                if (data == "Available") {

                    $("#result").html("<span style='color:green'>Available</span>");



                else {

                    $("#result").html("<span style='color:red'>Not Available</span>");








Now, look at the method in the Account controller.



public string CheckUserName(string input)


    bool ifuser = WebSecurity.UserExists(input);

    if (ifuser == false)


        return "Available";


    if (ifuser == true)


        return "Not Available";


    return "";



In case you wish to watch the video of this article, see the following:

Abhimanyu K Vatsa
Abhimanyu K Vatsa

Microsoft MVP | Author | IT Faculty | Student of M.Tech. IT | Blogs at ITORIAN.COM

Personal Blog:



Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.

Trending up