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

  • Home
  • SEARCH
  • 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 6033267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:27:32+00:00 2026-05-23T05:27:32+00:00

Is there anyway to use DataAnnotations in MVC 3 to not allow HTML is

  • 0

Is there anyway to use DataAnnotations in MVC 3 to not allow HTML is used in a textbox?
I see a way to allow using HTML (AllowHTMLAttribute) but what if i dont want the user to type any HTML in the textbox and want to warning him?

Thanks 🙂

  • 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-23T05:27:33+00:00Added an answer on May 23, 2026 at 5:27 am

    You have to write a custom RegularExpressionAttribute … something like this:

    public class DisallowHTMLAttribute : RegularExpressionAttribute
    {
        public DisallowHTMLAttribute()
            : base(@"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>")
        {
        }
    
        public override string FormatErrorMessage(string name)
        {
    
            return String.Format("The field {0} cannot contain html tags", name);
    
        }
    }
    

    You must register the adapter to enable client side validation, so in Application_Start in Global.asax add this line of code:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DisallowHTMLAttribute), typeof(RegularExpressionAttributeAdapter));
    

    And in your model, add the attribute to the properties you want to disallow html tags, like this:

    [DisallowHTML]
    public string SomeProperty{ get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway we can use Google App Engine but do not use google's
Is there anyway to use a bounded wildcard require a class implement more than
Is there anyway to use the Open Office SDK 2.0 to save a PowerPoint
Is there anyway to use atrm command to remove queue job from PHP web
Is there anyway to use LINQ to obtain the maximum of each columns for
Is there anyway I can use to destroy my deployed application at specific date?.
I was wondering if there was anyway to use functions like ReadFileEx that require
Is there anyway to make it possible to use .net 3.0 namespaces in a
Is there any way to use inheritance in database (Specifically in SQL Server 2005)?
Is there any way to use a constant as a hash key? For example:

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.