My Thoughts & Views

How to: Check login name availability in ASP.NET MVC using jQuery Validation plug-in

Posted on: April 1, 2009

I need to write comments for the below code, bcoz of lack of time i am posting only abstract things,if you want to know something, write comment, i will reply.

HTML login control

< input type="text" name="txtUserName" id="txtUserName" class="textBoxes" />

jQuery Rule: Add the below rule into script tag in header & add reference to jquery-1.2.6.js,jquery.validate.js in Head Element

$(document).ready(function() {

rules: {
required: true,
remote: { type: "post",
url: "Register/IsLoginAvailable" }

txtUserName: { required: "User Name is Required.",
remote: jQuery.format("{0} is not available.")}

The above validation code executed when the focus changes from txtUserName to some other, it will call IsLoginAvailable method available in the RegisterController, as specified in the url.

Controller Code :

Below code is the one which checks for availability of a username & returns the status to above jQuery Method. The following methods return type is JsonResult because remote method of jQuery accepts the JsonResult object only.

public JsonResult IsLoginAvailable(FormCollection collection)
JsonResult result = new JsonResult();

ilogIn.Text = this.GetLoginByUsername(collection.Get("txtUserName").

catch (Exception ex)

if (ilogIn.Text == collection.Get("txtUserName").ToString())
result.Data = false;
result.Data = true;

return result;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: