The Action Method CheckName contains the logic to check if the Username provided already exists or not. The code provided here is only for demo. Thread.Sleep(3000) will simulate a delay as if to show that the application is working really hard to find out if the username exists or not. REMOVE that line when you are writing the actual code to check the username against a database or Web service.

NOTE: The return value need not be a JsonResult. You can change it to ActionResult and return some string value to indicate the success/failure of the call.

Step 3: Lets replace the comment Your jQuery code will come here from our view with the following jQuery code…

The code is very simple. The button click event makes an AJAX call to our ActionMethod CheckName and expects a response. The Thread.Sleep(3000) will delay the response for 3 seconds. The ActionMethod checks the value of the username and returns a JSON string for our AJAX call to handle.

We can fancy it up a bit by providing some color to our status messages using the jQuery addClass method. Putting it all together, our View will looks as follows…

If you are using ASP.NET MVC then $(“#TEXTBOXID”) will get you the handler to the text box.

In case of ASP.NET Webforms the ClientID is generated at runtime. For instance the ID of the text box would be something like ctl01_Content1_txtUserName. To access this text box in jQuery you would have to do something like this…

Thanks for your reply. Can you please tell what is ‘Home’ in following line. Is it a directory or some mvc convention. I have my test.aspx file which has the text box and script etc in main directory and the UniqueNameController.cs file in App_code folder in visual studio.
$.post(“/Home/CheckName”, { username: name },

i am usually jumping on the online world the majority of the night which means that I have the inclination to read significantly, which unfortunately isn’t typically a beneficial factor as the largest part of the web sites I discover are constructed of pointless crap copied from some other internet websites a thousand times, on the other hand I gotta say this blog is definitely enjoyable and holds some authentic information, so thank you for stopping the phenomena of solely duplicating other people’s blogs and forums, if you ever wanna have fun with playing a few hands of facebook poker together with me let me know – you have my email address🙂

Nice to see all your sharings. And am recently completed my DotNet course and intrested to share things like you .So please help me out by giving good materials to me..Right now am working as Trainee in AJTech ..

Hey I just wanted to let you know, I really like the written material on your web site. But I am employing Firefox on a machine running version 9.10 of Ubuntu and the look and feel is not quite right. Not a strong deal, I can still essentially read the posts and explore for information, but just wanted to inform you about that. The navigation bar is kind of hard to use with the config I’m on. Keep up the great work!

i tried the code in my MVC application for email validation, at it didnt worked, but after removing the threading in the controller it works fine……
please if possible can you tell me the reason for that….