MVC3:使通过jQuery所需的复选框验证? [英] MVC3: make checkbox required via jQuery validate?

查看:104
本文介绍了MVC3:使通过jQuery所需的复选框验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的同意条款复选框使用jQuery验证,在MVC3项目是强制性的。我目前从MS数据注解属性+MS MVC3不显眼的jQuery验证获取服务器/客户端DRY / SPOT验证。

I want my "Agree To Terms" checkbox to be mandatory using jQuery validate, in an MVC3 project. I currently get server/client DRY/SPOT validation from "MS data annotation attributes" + "MS MVC3 unobtrusive jQuery validation".

下面是一个独立的测试(平原MVC3生成的HTML)。为什么它不工作,请?运行时,验证确保联系人姓名字段被填充,但不关心复选框的状态。

Here's a stand-alone test (plain HTML generated by MVC3). Why doesn't it work, please? When run, validation ensures the "Contact Name" field is filled, but doesn't care about the state of the checkbox.

<!DOCTYPE html>

<html>
<head>
    <title>RequiredCheckbox</title>

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="//ajax.microsoft.com/ajax/jQuery.Validate/1.7/jQuery.Validate.js"></script>
    <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js"></script>
    <script type="text/javascript" language="javascript">
        $(function () {
            // http://itmeze.com/2010/12/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/
            $.validator.unobtrusive.adapters.add("mandatory", function (options) {
                    options.rules["required"] = true;
                    if (options.message) {
                        options.messages["required"] = options.message;
                    }
                }
            });
            $.validator.unobtrusive.parse(document);
        });
    </script>

</head>

<body>
    <div>
        <form>
            <input data-val="true" data-val-mandatory="The field Terms Are Accepted is invalid." id="isTermsAccepted" name="isTermsAccepted" type="checkbox" value="true" />
            <input name="isTermsAccepted" type="hidden" value="false" />
            <span class="field-validation-valid" data-valmsg-for="isTermsAccepted" data-valmsg-replace="true"></span>

            <input data-val="true" data-val-required="The Contact Name field is required." id="contactName" name="contactName" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="contactName" data-valmsg-replace="true"></span>
            <button type="submit">Submit</button>
        </form>
    </div>
</body>
</html>

搜索结果
结果




设定数据标注属性[要求]没有帮助:搜索
    <一href=\"http://forums.89.biz/forums/MVC+3+Unobtrusive+validation+does+not+work+with+checkboxes+(jquery+validation)+and+the+fix+for+it\">http://forums.89.biz/forums/MVC+3+Unobtrusive+validation+does+not+work+with+checkboxes+(jquery+validation)+and+the+fix+for+it.

Setting data annotation attribute [required] doesn't help:
http://forums.89.biz/forums/MVC+3+Unobtrusive+validation+does+not+work+with+checkboxes+(jquery+validation)+and+the+fix+for+it.

这很好。什么是需要是指一个复选框,显然是一个神圣的战争我不想涉进,其中MS以为他们知道比jQuery开发团队更好。强迫在本地应该是一件简单的事情:结果
     $(形式)验证({规则:{cbAgreeToTerms:需要}});

That's fine. What "required" means for a checkbox is obviously a holy war I don't want to wade into, where MS thought they knew better than the jquery team. Coercing it locally should be a simple matter of:
$("form").validate({ rules: { cbAgreeToTerms: "required" } });

...对吧?没有,因为:结果
    <一href=\"http://blog.waynebrantley.com/2011/01/mvc3-breaks-any-manual-use-of-jquery.html\">http://blog.waynebrantley.com/2011/01/mvc3-breaks-any-manual-use-of-jquery.html

    <一href=\"http://pinoytech.org/question/4824071/microsofts-jquery-validate-unobtrusive-makes-other-validators-skip-validation\">http://pinoytech.org/question/4824071/microsofts-jquery-validate-unobtrusive-makes-other-validators-skip-validation

...right? no, because of:
http://blog.waynebrantley.com/2011/01/mvc3-breaks-any-manual-use-of-jquery.html
http://pinoytech.org/question/4824071/microsofts-jquery-validate-unobtrusive-makes-other-validators-skip-validation

什么呢?这是pretty臭cheezy! (恕我直言,当然。)

WHAT? That's pretty stinking cheezy! (IMHO, of course.)

现在,我已经试过此解决方案:结果
    <一href=\"http://itmeze.com/2010/12/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/\">http://itmeze.com/2010/12/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/

并没有为我工作。笔者的意见晃来晃去,有点货物邪教利用倒复选框试验的早期在他/她的文章让我怀疑它的实际工作为他/她,然后参与其他什么巫术?

Now, I've tried this solution:
http://itmeze.com/2010/12/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/
and it didn't work for me. This author's dangling comments and somewhat cargo-cult use of the inverted CHECKBOX test from earlier in his/her article make me wonder if it actually works for him/her, then what other voodoo was involved?

请注意我认为,JS的最后一个片段是相当于清洁剂:结果
     $ validator.unobtrusive.adapters.addBool(brequired,要求);
这一建议在过去后:结果
    <一href=\"http://forums.asp.net/p/1648319/4281140.aspx#4281140\">http://forums.asp.net/p/1648319/4281140.aspx#4281140

但是请注意,作者评论说,他没有调试呢。它没有工作对我来说,阅读字里行间,我想他的意思是它没有为他工作?

Note I think that the last snippet of JS is equivalent to the cleaner:
$.validator.unobtrusive.adapters.addBool("brequired", "required"); That was suggested by the last post in:
http://forums.asp.net/p/1648319/4281140.aspx#4281140
But notice that author comments that he hadn't debugged it yet. It didn't work for me, and reading between the lines, I think he means it didn't work for him?

该unobtrusive.js通话解析上docready,所以我试图调用一个,但它并没有帮助我。结果
     $ validator.unobtrusive.parse(文件);
我还发现了一些类似的文章,并没有谈论要求任何形式的初始化。也许他们都在本地编辑原始/公共unobtrusive.js?我宁愿不,如果我能帮助它,这不正是适配器是?

The unobtrusive.js calls parse on docready, so I tried calling that, but it didn't help me.
$.validator.unobtrusive.parse(document); I've also found a few similar articles and none talk about requiring initialization of any sort. Maybe they are all locally editing the original/public unobtrusive.js? I'd rather not if I can help it, isn't that what the adapters are for?

我发现堆栈溢出文章,大体相同,以及更复杂的例子:搜索
    <一href=\"http://stackoverflow.com/questions/4833280/asp-net-mvc-3-unobtrusive-custom-client-validation\">ASP .NET MVC 3不显眼的自定义客户端验证。结果
    <一href=\"http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute\">Perform自定义属性结果客户端验证
    <一href=\"http://xhalent.word$p$pss.com/2011/01/27/custom-unobstrusive-jquery-validation-in-asp-net-mvc-3/\">http://xhalent.word$p$pss.com/2011/01/27/custom-unobstrusive-jquery-validation-in-asp-net-mvc-3/

但我没有看到任何有比我已经尝试过不同的。

I found stack-overflow articles, much the same, as well as more complex examples:
ASP .Net MVC 3 unobtrusive custom client validation.
Perform client side validation for custom attribute
http://xhalent.wordpress.com/2011/01/27/custom-unobstrusive-jquery-validation-in-asp-net-mvc-3/
But I don't see anything there that is different than what I've already tried.

这是真的工作的人吗?为什么我不能让它为我工作?

Is this really working for people? Why can't I get it to work for me?

推荐答案

只是你的JavaScript改成这样:

Just change your javascript to this:

(function ($) {
    // http://itmeze.com/2010/12/checkbox-has-to-be-checked-with-unobtrusive-jquery-validation-and-asp-net-mvc-3/
    $.validator.unobtrusive.adapters.add("mandatory", function (options) {
        options.rules["required"] = true;
        if (options.message) {
            options.messages["required"] = options.message;
        }                
    });            
} (jQuery));

您实际上并不需要,虽然编写自己的适配器,可以只使用:

You do not actually need to write your own adapter though and can just use:

(function ($) {
    $.validator.unobtrusive.adapters.addBool("mandatory", "required");
} (jQuery));

这篇关于MVC3:使通过jQuery所需的复选框验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆