Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:24:23+00:00 2026-05-23T18:24:23+00:00

I’m trying to write a custom validation that gives an error if html exists

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:24:24+00:00Added an answer on May 23, 2026 at 6:24 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.