最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom jquery validation and unobtrusive JavaScript - Stack Overflow

programmeradmin1浏览0评论

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.

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

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.

text 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>
Share Improve this question edited Feb 5, 2014 at 19:01 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Mar 24, 2011 at 21:29 raklosraklos 28.5k60 gold badges189 silver badges306 bronze badges 2
  • are you using some kind of validation plugin or something similar? – thwd Commented Mar 24, 2011 at 21:36
  • im using jquery.validate.min.js – raklos Commented Mar 24, 2011 at 21:38
Add a comment  | 

2 Answers 2

Reset to default 16

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.

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.

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.

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>

and the javascript needed for the unobtrusive is as follows

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

A brief comparision of Traditional vs Unobtrusive JavaScript Validation in MVC 3 - Mitchell Trent's Blog
ASP.NET MVC 3 Custom Validation - Microsoft Developer Network

Although I haven't tested this, I think all that you're missing is to wire up the element that you want to validate with the nohtml rule. Something like this:

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

Based on some of your description, I assume you're using 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 (e.g. <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.

发布评论

评论列表(0)

  1. 暂无评论