我在用ASP.NET MVC 3,带有数据注释和jQuery验证插件.

有没有一种方法可以标记某个字段(或某个数据注释)应该只在服务器端进行验证?

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.

推荐答案

I'm not sure if this solution works on MVC3. It surely works on MVC4:

只需在渲染字段之前在Razor视图中禁用客户端验证,并在渲染字段之后重新启用客户端验证.

例子:

<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.

Jquery相关问答推荐

如何在不使用点击事件 bootstrap 程序模式的情况下使用弹出窗口 bootstrap 程序显示用户详细信息?

javascript 捕获浏览器快捷方式 (ctrl+t/n/w)

如何在 jQuery 中使用:not 和 hasClass() 来获取没有类的特定元素

递归循环遍历对象(树)

jquery如何捕获输入键并将事件更改为选项卡

在 jQuery UI Datepicker 中禁用future 日期

未捕获的 TypeError:data.push 不是函数

页面重新加载后,如何使用 twitter bootstrap 保持当前选项卡处于活动状态?

jQuery DataTables:延迟搜索直到输入 3 个字符或单击按钮

如何检测是否安装了 Flash,如果没有,显示一个隐藏的 div 通知用户?

jQuery 如果 div 包含此文本,则替换该部分文本

虽然未定义变量 - 等待

CORS POST 请求可以使用纯 JavaScript,但为什么不使用 jQuery?

与 C# HashSet 等效的 JavaScript 是什么?

检测夹点的最简单方法

如何在纯 JavaScript 中模拟鼠标悬停以激活 CSS:悬停?

删除除一个之外的所有类

如果一个元素正在被动画,我如何用 jQuery 找出?

Jquery Select 所有具有 $jquery.data() 的元素

如何使用 bootstrap 中的 selectpicker 插件在 Select 时设置选定值