Eagerly Performing ASP.NET MVC 3 Unobtrusive Client Side Validation

Unobtrusive client side validation is one of the great feature that I like in ASP.NET MVC 3. Unobtrusive client side validation feature uses the famous jQuery validation plug-in internally. Jquery validation plug-in perform client side validation lazily. What does this means? This simply means that before submitting the form for the first time, the user can tab through fields without getting any error message. This makes sense and lot of developers and designers like this behavior. But guys coming from places where validation is performed when fields focus out, may not like this behavior. In this article, I will show you how to perform validation on fields when fields focus out.

Description:

Firstly, let's see the default behavior in action. Create a new ASP.NET MVC 3 application. Then just open(or create) HomeController.cs file and add the following code,

public ActionResult Index()
{
return View();
}

Next create a new class file UserInformation.cs inside Model folder and add the following code,

Now just run this application. You will see jQuery validation plug-in perform client side validation lazily, i.e, users can tab through fields without getting any error message before submitting the form for the first time. Now for performing validation eagerly, i.e, performing validation each time when users fields focus out, you need to add this script at bottom of the page.

Now, just run your application again. You will see the validation behavior become eager. Now, let's say that you need to add this behavior to all of your views. Adding this script to all of your views may be pain for you. For making this behavior possible in all of your views without adding this script to every view, you need to open jquery.validate.unobtrusive(.min).js file and then replace this script,

By default, ASP.NET MVC 3 leverage the famous jQuery validation plug-in to perform client side validation. This famous validation plug-in lazily perform client side validation. Lot of peoples like this behavior but many people do not. In this article, I showed you how you can make the client side validation eager. I am also attaching a sample application. Hopefully you will enjoy this article too.

my requirements were not any of these , but then my validators failed to fire when control lost focus for some reason. tried everything on the stack overflow and asp.net forum none helped me for some reason.