I'm using ASP .NET MVC 3 with Data Annotations and the jQuery validate plugin.
Is there a way to mark that a certain field (or certain data annotation) should only be validated server-side?
I have a phone number field with a masking plugin on it, and the regular expression validator goes crazy on the user's end. The regex is only a fail-safe (in case someone decides to hack the javascript validation), so I don't need it to run on the client side. But I'd still like the other validation to run client side.
To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.
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. For client-side validation, the values of above both the keys must be true.
Firstly, you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application, Open Visual Studio choose File, New, then Project. It will open a New Project window, from where you need to choose node Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application.
I'm not sure if this solution works on MVC3. It surely works on MVC4:
You can simply disable client side validation in the Razor view prior to render the field and re-enable client side validation after the field has been rendered.
Example:
<div class="editor-field">     @{ Html.EnableClientValidation(false); }     @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })     @{ Html.EnableClientValidation(true); } </div>   Here we disable client side validation for the BatchId field.
Also I have developed a little helper for this:
public static class YnnovaHtmlHelper {     public static ClientSideValidationDisabler BeginDisableClientSideValidation(this HtmlHelper html)     {         return new ClientSideValidationDisabler(html);     } }  public class ClientSideValidationDisabler : IDisposable {     private HtmlHelper _html;      public ClientSideValidationDisabler(HtmlHelper html)     {         _html = html;         _html.EnableClientValidation(false);     }      public void Dispose()     {         _html.EnableClientValidation(true);         _html = null;     } }   You will use it as follow:
<div class="editor-field">     @using (Html.BeginDisableClientSideValidation()) {         @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })     } </div>   If anyone has better solutions please let me know!
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With