What's the best way to implement field validation using ASP.NET MVC?

Take a look at the JQuery Validation plugin this plugin is amazing,it's clean to implement and has all the features you could ever need, including remote validation via AJAX.

Also a sample MVC controller method can be found here which basically uses the JsonResult action type like:

public JsonResult CheckUserName(string username)
{
    return Json(CheckValidUsername(username));
}

IMO using xVal with jQuery and DataAnnotationsModelBinder is the best combination.

Sometimes however, there are validation rules which cannot be checked entirely on the client side, so you need to use remote client-side validation.

I figured out way to generically implement remote client-side validation with xVal / jQuery.validate so that

  • Validation rules remain solely in your ASP.NET MVC model
  • You write each validation rule just once, and only in easily testable C# code. There is no JavaScript or other client-side counterpart .
  • There is no need to branch or otherwise modify xVal or jquery.validate
  • All you have to do for each new remote form validation rule is to derive from the base class shown in this article.

I wrote a blog article on this describing all the details.