简体   繁体   English

自定义jquery验证和不引人注目的JavaScript

[英]custom jquery validation and unobtrusive JavaScript

I'm trying to write a custom validation that gives an error if html exists in the textarea when they submit a form. 我正在尝试编写一个自定义验证,如果在提交表单时textrea中存在html,则会产生错误。

I have the following - its not working and I'm not sure why. 我有以下 - 它不工作,我不知道为什么。

also I don't understand the unobtrusive part can someone show me how to do that as I am seeing other examples on SO that have it. 我也不明白这个不引人注目的部分可以告诉我如何做到这一点,因为我正在看到有关它的其他例子。

text area has a class"note" the form is called "noteform" 文本区域有一个“注释”类,该表单称为“noteform”

  <script type="text/javascript" >
        $(document).ready(function () {


        $.validator.addMethod('nohtml', function (value, element) {
            var text = $(".note").text();
            if ($(text).length > 0) {
                return false;
            }
            else {
                return true;
            }
        }, 'Html not allowed');

    //    // **not sure what to do here**
    //    $.validator.unobtrusive.adapters.add('containsnohtml', {}, function (options) {
    //        options.rules['nohtml'] = false;
    //        options.messages['nohtml'] = options.message;
    //    });

        $('#noteform').validate({
            rules: { nohtml: "required nohtml" }
        });

    });

</script>

There's a couple issues here. 这里有几个问题。 One is you're trying to mix unobtrusive and regular jquery validation. 一个是你试图混合不引人注目和常规的jquery验证。 If you want to use validate like this then you need to make sure jquery.validate.unobtrusive.js is NOT included. 如果你想使用这样的验证,那么你需要确保不包括jquery.validate.unobtrusive.js This is because jquery.validate.unobtrusive.js automatically parses and produces a validator for the document and the very first thing that validate does is check if there's an existing validator and exits if there is. 这是因为jquery.validate.unobtrusive.js自动解析并生成文档的验证器, validate第一件事是检查是否存在现有验证器,如果有,则退出。

If you do decide to go the non-unobtrusive route, be sure not to use the $.validator.unobtrusive.adapters.add since it will cause an error without jquery.validate.unobtrusive.js . 如果你决定采用非不引人注意的路线,请确保不要使用$.validator.unobtrusive.adapters.add因为它会导致错误而没有jquery.validate.unobtrusive.js

I would recommend going with unobtrusive validation though since I think you're using MVC3. 因为我认为你正在使用MVC3,所以我建议使用不引人注目的验证。 If you're going to go with unobtrusive validation you have two choices, set the data-* attributes yourself by adding data-val="true" data-val-nohtml="Html not allowed" to your textarea as suggested by JohnnyO and including a span with data-valmsg-for="note" data-valmsg-replace="true" to show the error message. 如果您要进行不显眼的验证,您有两个选择,通过将约翰尼的建议添加data-val="true" data-val-nohtml="Html not allowed"到您的textarea来自己设置data- *属性。包括带有data-valmsg-for="note" data-valmsg-replace="true"的span,以显示错误消息。 Or you can make your own DataAnnotation attribute. 或者您可以创建自己的DataAnnotation属性。

Here's the code for the addMethod (needed for both kinds of validation) 这是addMethod的代码(两种验证都需要)

 <script type="text/javascript">
     (function ($) {
            $.validator.addMethod('nohtml', function (value, element) {
                // Test 'value' for html here. 'value' is the value of the control being validated.
                return true; // Return true or false depending on if it passes or fails validation, respectively.
            }, 'Html not allowed');

        } (jQuery));
    </script>

and the javascript needed for the unobtrusive is as follows 并且不引人注目的javascript如下

$.validator.unobtrusive.adapters.addBool('nohtml');

Regarding how to make a custom validation attribute, since I'm not sure what language you're using, assuming you're using MVC3, or if you even need this info anymore 4 months after you asked, I'm going to simply leave these links for reference. 关于如何制作自定义验证属性,因为我不确定你正在使用什么语言,假设你正在使用MVC3,或者你甚至在问了4个月之后甚至还需要这些信息,我只想离开这些链接供参考。

A brief comparision of Traditional vs Unobtrusive JavaScript Validation in MVC 3 - Mitchell Trent's Blog 在MVC 3中简要比较传统与不引人注目的JavaScript验证 - Mitchell Trent的博客
ASP.NET MVC 3 Custom Validation - Microsoft Developer Network ASP.NET MVC 3自定义验证 - Microsoft Developer Network

Although I haven't tested this, I think all you're missing is to wire up the element that you want to validate with the nohtml rule. 虽然我没有对此进行过测试,但我认为您所缺少的是使用nohtml规则连接要验证的元素。 Something like this: 像这样的东西:

$('textarea.note').rules('add', {
  nothml: true
});

Based on some of your description, I assume you're using ASP.NET MVC3. 基于您的一些描述,我假设您使用的是ASP.NET MVC3。 In that case, you would only need to use the unobtrusive adapter if you're generating the validation attributes server side on your html element (eg <textarea data-val="true" data-val-nohtml="Html not allowed"></textarea> ). 在这种情况下,如果您在html元素上生成验证属性服务器端,则只需要使用不显眼的适配器(例如<textarea data-val="true" data-val-nohtml="Html not allowed"></textarea> )。 In such a case, you'll need the unobtrusive adapter to wire up the element to use your nohtml rule. 在这种情况下,您需要使用不显眼的适配器连接元素以使用您的nohtml规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM