How to enable and disable client-side validation in ASP.NET MVC?

We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level.

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

For client-side validation, the values of above both the keys must be true. When we create new project using Visual Studio in MVC3 or MVC4, by default the values of both the keys are set to true.
We can also enable the client-side validation programmatically. For this we need to do code with in the Application_Start() event of the Global.asax, as shown below.

protected void Application_Start()
		{
			//Enable or Disable Client Side Validation at Application Level
			HtmlHelper.ClientValidationEnabled = true;
			HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
		}

We can also enable or disable client-side validation for a specific view. For this we required to enable or disable client side validation inside a Razor code block as shown below. This option will overrides the application level settings for that specific view.

@using MvcApp.Models
@{
    ViewBag.Title = "About";
    HtmlHelper.ClientValidationEnabled = false;
}
Tagged , . Bookmark the permalink.

Leave a Reply