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 8030837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:53:46+00:00 2026-06-05T00:53:46+00:00

I am getting an HTML encoded value out of MVC’s [RegularExpression] validator when it

  • 0

I am getting an HTML encoded value out of MVC’s [RegularExpression] validator when it writes the code to the client. This isn’t an issue for the error message (since it should be encoded for later display), but it is really screwing up the regex 🙂 On a string field, my regular expression looks like this:

[RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")]

When this gets written out by mvc3, it shows up looking like this:

<input ... data-val-regex-pattern="^[^\&amp;lt;\&amp;gt;]*$" 
           data-val-regex="May not contain &amp;lt;,&amp;gt;" .../>

Edit:

  • Because of the encoding, it was also catching <>, but also tripping up on words with:
  • t like tim
  • g like goat
  • probably ; but I didn’t test it

The purpose of this regex is to filter out < and >, rather than disable all validation on the page and preform it on the server side. This field accepts multiple unicode langages, and is only 12 chars long.

My choices look like:

  1. Disable asp.net input sanity checks on one page and check that in the action only
  2. Find a complex regex to match 3+ unicode ranges +/- dashes & numbers – .net + js compat.
  3. Write a custom regex validator that doesn’t have client side validation
  4. Use a [Remote] validator to do it on the server side like we do for other fields

I’m leaning towards #3 now, but I would really like to find a way to keep to built-in functionality. Is there any way to disable this output escaping?

  • 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-06-05T00:53:47+00:00Added an answer on June 5, 2026 at 12:53 am

    It took me a while, but I found out what seems obvious now: use unicode literals instead of the actual characters <>.

    My regex ended up like this, and works in both .Net & JS

    // \u003c = <,  \u003e = >
    [RegularExpression(@"^[^\u003C\u003E]*$", ErrorMessage = "...")]
    

    I ended up with a custom validation attribute to keep from having that code everywhere. Better would be a custom “default” object adapter & custom jquery validation for this regex, thus allowing a second regex for format to be attached. Work for another day. Here is the custom class & validation:

    Custom subclass of regular expression attribute:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class RegexUnsafeChars : RegularExpressionAttribute
    {
        public RegexUnsafeChars() : base(@"^[^\u003C\u003E]*$")
        {
            base.ErrorMessage = "May not contain <,>";
        }
    }
    

    Because this is a custom attribute, and the provider only checks for EXACT matches, we need to declare the adapter manually. Thankfully we can re-use the built-in regex one. Add this code in Application_Start() in Global.asax.cs

    DataAnnotationsModelValidatorProvider.RegisterAdapter(
                typeof(RegexUnsafeChars), 
                typeof(RegularExpressionAttributeAdapter));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
I'm getting the Potentially dangerous Request.Form request value was detected from the client exception
I am getting following encoded html as a json response and has no idea
the tigris website http://subversion.tigris.org/getting.html#windows has a link to another svn server, which is called
I am getting a HTML string as a result of querying a remote XML
I am getting some HTML text from a remote server which I am displaying
Happstack Lite is breaking on me because it's getting blaze-html version 0.5 and it
I'm writing a Javascript app that is getting an HTML document via AJAX, then
I'm trying to follow the rails getting started guide http://guides.rubyonrails.org/getting_started.html to create a blog
I am trying to install Googles' eclipse plugin - http://code.google.com/eclipse/docs/getting_started.html Even on following all

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.