本文介绍了自定义jquery验证和不引人注目的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试编写自定义验证,如果在提交表单时textrea中存在html,则会出现错误。I'm trying to write a custom validation that gives an error if html exists in the textarea when they submit a form.我有以下内容 - 它不工作,我不知道为什么。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. 文本区域有一个类注表格被称为noteformtext area has a class"note"the form is called "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> 推荐答案这里有几个问题。一个是你试图混合不引人注目和常规的jquery验证。如果你想使用这样的验证,那么你需要确保不包括 jquery.validate.unobtrusive.js 。这是因为 jquery.validate.unobtrusive.js 自动解析并生成文档的验证器,并且第一件事就是验证确定是否存在现有验证器,如果存在则退出。There's a couple issues here. One is you're trying to mix unobtrusive and regular jquery validation. If you want to use validate like this then you need to make sure jquery.validate.unobtrusive.js is NOT included. 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.如果您决定采用非不显眼的路线,请务必不要使用 $。validator.unobtrusive.adapters.add 因为没有 jquery.validate.unobtrusive.js 会导致错误。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.我建议使用不引人注目的验证,因为我认为你使用的是MVC3。 如果您要进行不显眼的验证,您有两种选择,通过添加 data-val =truedata-val-nohtml =Html not来自己设置data- *属性根据JohnnyO的建议允许到您的textarea,并包含一个带有的数据 - 数据-valmsg-for =note数据-valmsg-replace =true显示错误消息。或者您可以创建自己的DataAnnotation属性。I would recommend going with unobtrusive validation though since I think you're using 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. Or you can make your own DataAnnotation attribute.这是addMethod的代码(两种验证都需要)Here's the code for the addMethod (needed for both kinds of validation) <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>并且不显眼所需的javascript如下and the javascript needed for the unobtrusive is as follows$.validator.unobtrusive.adapters.addBool('nohtml');关于如何制作自定义验证属性,因为我不确定您使用的是哪种语言,假设您使用的是MVC3,或者您在询问后4个月甚至不再需要此信息,我将简单地留下这些链接以供参考。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. 传统与不引人注意的简短比较MVC 3中的JavaScript验证 - Mitchell Trent的博客 ASP。 NET MVC 3自定义验证 - Microsoft Developer Network 这篇关于自定义jquery验证和不引人注目的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 22:08