Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since entering html in a TextArea results in an HttpRequestValidationException exception being thrown, early in the pipeline, we can only catch it along with unhandled exceptions in Global.asax.</p> <p>In Global.asax.cs we add:</p> <pre><code> void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); ex = ex.InnerException ?? ex; if (ex is HttpRequestValidationException) { string url = Request.Url.ToString() + "?error=1"; Response.Redirect(url); Server.ClearError(); return; } //any other exception handling that you need goes here } </code></pre> <p>Here is the markup:</p> <pre><code>&lt;form action="&lt;%=Url.Action("Create") %&gt;" method="post" class="data-entry-form" id="feedBackForm"&gt; &lt;fieldset class="comment"&gt; &lt;div class="editor-field"&gt; &lt;%= Html.TextAreaFor(model =&gt; model.Comment, 10, 2, new { placeholder="your message" }) %&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.Comment) %&gt; &lt;% if (Request.QueryString["error"] == "1") { Response.Write("&lt;br/&gt;&lt;span class= 'error'&gt;Please remove all HTML from your comment and resubmit&lt;/span&gt;"); } %&gt;&lt;/div&gt; &lt;br /&gt; E-mail address (optional) &lt;div class="editor-field"&gt; &lt;%= Html.TextBoxFor(model =&gt; model.Email, new { placeholder="you@youremailaddress.com" }) %&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.Email) %&gt; &lt;/div&gt; &lt;input type="submit" value="Send" /&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>notice the line: <code>if (Request.QueryString["error"] == "1"</code> handles the parameter passed in the redirect in Application_error</p> <p>So far we have server side validation.</p> <p>For client side validation, we add a custom rule using the JQuery Validate plugin:</p> <pre><code> jQuery.validator.addMethod("hasNoHTML", function (value, element) { if (value.match(/&lt;(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^&gt;\s]+))?)*)\s*(\/?)&gt;/)) { return false; } return true; }, "* Please remove all HTML from your comment and resubmit"); $("#feedBackForm").validate( { rules: { Comment: { required: true, hasNoHTML: true } } } ); </code></pre> <p>Here is the reference to the regular expression: <a href="http://ejohn.org/files/htmlparser.js" rel="nofollow">http://ejohn.org/files/htmlparser.js</a></p> <p>And the css to decorate the error: </p> <pre><code>.error { color:red; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload