“<” in a text box in ASP.NET --> how to allow it?

前端 未结 7 2078
粉色の甜心
粉色の甜心 2020-12-01 18:24

I have a textfield which displays a string which contains < and >. The code throws an error because of that. How can I allow the usage of those chars in my textfield?

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 19:13

    The easiest solution is to disable request validation in single pages

    <%@ Page ... ValidateRequest="false" %>
    

    but don't forget to enable requestValidationMode="2.0"

    
       ...
       
    
    

    This solution could espose some threats.


    Another smart solution is to replace via javascript text written by user to make it safe for validation: is considere dangerous, but < tag> is considered safe!

    A javascript replacement can solve the problem.

    function validateTxt() {
        $("textarea, input[type='text']").change(function () {
          html = $(this).val(); //get the value
          //.replace("a" , "b")  works only on first occurrence of "a"
          html = html.replace(/< /g, "<"); //before: if there's space after < remove
          html = html.replace(/

提交回复
热议问题