如何让文本框接受html标签

发布于 2024-12-14 03:18:53 字数 690 浏览 0 评论 0原文

我的 aspx 页面中有一个文本框。用户希望能够在文本框中输入html标签。到目前为止,我知道绕过验证错误的唯一方法是将 ValidateRequest 设置为 false。我尝试了其他几种方法: (1) 在这样的 javascript 中使用 Server.HtmlEncode

<%@ Import Namespace="System.Web" %>

        var tb = document.getElementById("<%=synopsisTextBox.ClientID%>");
        var value =Server.HtmlEncode(tb.value);

但我遇到了编译器错误。谁能告诉我我做错了什么?

(2)创建我自己的编码函数

function escapeHTML (str) 
{ 
   var div = document.createElement('div'); 
   var text = document.createTextNode(str); 
   div.appendChild(text); 
   return div.innerHTML; 
}

不知何故它工作得不太好。当我单击某个导致同一页面中回发的按钮时,它会卡住。

谁能告诉我是否有更好的方法让你的文本框接受 html 标签?谢谢。

I have a textbox in my aspx page. The user wants to have the ability to input html tags in the textbox. The only way so far I know to by pass the validation error is set the ValidateRequest to false. I tried couple of other ways:
(1) using Server.HtmlEncode in a javascript like this

<%@ Import Namespace="System.Web" %>

        var tb = document.getElementById("<%=synopsisTextBox.ClientID%>");
        var value =Server.HtmlEncode(tb.value);

But I got compiler error. Can anyone tell me what I did wrong?

(2) creating my own encode function

function escapeHTML (str) 
{ 
   var div = document.createElement('div'); 
   var text = document.createTextNode(str); 
   div.appendChild(text); 
   return div.innerHTML; 
}

somehow it didn't work quite well. When I click some button that causes postback in the same page, it gets stuck.

Can anyone tell me if there is a better way to make your textbox accept html tags? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你曾走过我的故事 2024-12-21 03:18:53

只需禁用输入验证(仅适用于页面)并确保对页面中其他文本框的输入进行编码。默认情况下,输入验证处于启用状态,并不是因为它不应该被禁用,而是因为您应该知道自己在做什么并明确执行。这样您就一定会注意并进行自己的输入验证。

Just disable input validation (for the page only) and make sure you encode input from other textboxes in the page. Input validation is on by default not because it shouldn't be disabled ever but because you should know what you are doing and do it explicitly. This way you are sure to pay attention and do your own input validation.

趴在窗边数星星i 2024-12-21 03:18:53

如果您需要对 html 标签进行编码并在浏览器上显示它们,请记住这

 <%= Server.HTMLEncode("The paragraph tag: <P>") %>  

会产生以下输出:

The paragraph tag: <P>

Web 浏览器将显示为:

The paragraph tag: <P>

您对 Server.HTMLEncode 的试用无效,因为它有效当数据位于服务器上并且需要在发送到浏览器之前进行编码时。在您的示例中,数据位于浏览器上,请求在服务器接收之前会在验证时被阻止


如果您希望用户编辑文本框并输入 html 标签,您可以通过

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

或在整个应用程序的 web.config 中禁用此功能:

<system.web>
  <page validateRequest="false" />
</system.web>

请注意,如果没有此 ValidateRequest 属性,则该属性不存在
原因。当您更改其默认值时,将出现不安全的输入
公认。因此,您需要验证每个用户的输入
避免跨站点脚本攻击,例如插入恶意代码
JavaScript、ActiveX、Flash 或 HTML


另一个智能解决方案是通过用户编写的 JavaScript 文本进行替换,以确保验证安全。
<代码>< tag> ,而不是 被认为是安全的!

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(/</g, "< "); // add space after <
      $(this).val(html); //set new value
   });
}

$(document).ready(function () {
      validateTxt();
});

If you need to encode html tags and show them on a browser remember that

 <%= Server.HTMLEncode("The paragraph tag: <P>") %>  

produces the following output:

The paragraph tag: <P>

that will be displayed by a Web browser as:

The paragraph tag: <P>

Your trial with Server.HTMLEncode is not working, since it works when data are on the server and need to be encoded before sending to the browses. In your sample, data are on the browser and request is blocked on validation before being recieved on the server .


If you want user to edit TextBox and enter html tags you can disable this via

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

or in the web.config for your entire application:

<system.web>
  <page validateRequest="false" />
</system.web>

Note that this ValidateRequest property is not existing without
reason. When you change its default value, insecure input will be
accepted. Because of that, you need to validate every user's input to
avoid cross-site scripting attacks, like inserting of malicious
JavaScript, ActiveX, Flash or HTML


Another smart solution is to replace via javascript text written by user to make it safe for validation.
< tag> , instead of <tag> is considered safe!

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(/</g, "< "); // add space after <
      $(this).val(html); //set new value
   });
}

$(document).ready(function () {
      validateTxt();
});
ヤ经典坏疍 2024-12-21 03:18:53

IMO,您有以下解决方案:

  • 遵循@Stilgar并使用Anti-XSS进行编码。
  • 使用 Textile 或 BBCode 标记语言而不是 HTML。

IMO, you have these following solutions:

  • Follow @Stilgar and also use Anti-XSS for encoding.
  • Use Textile or BBCode markup language instead of HTML.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文