当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Storing and Displaying TextArea user inputs

问题描述:

How can i handle user inputs with textarea, i need to strip user entered html tags, store text somewhere and display it back in a webpage.

I also need to take care about line breaks

Any best practices without using <pre> tag ?

网友答案:

You can always do a find-replace of \n with <br /> to preserve line breaks.

However, stripping html is a bit trickier. The easiest thing to do is replace < and > with &lt; and &gt;. But that doesn't actually strip the html, it merely forces it to render as plain text instead of html.

You could use a regex replace to remove <anything> but that has many potential pitfalls.

网友答案:

if you're using PHP, you can always use the nl2br() function to display the text back on the page.

网友答案:

I created a function called SafeComment designed to eliminate the problem characters from the input for SQL, javascript, HTML and VB. Since our sites and code are almost all VB & VB script. It's function is to allow any freeform input field to be successfully received, processed, saved and displayed. We needed it to maintain PCI compliance. It's not pretty, but it works.

Function SafeComment(ByVal strInput)
' Renders Any Comment Codes Harmless And Leaves Them HTML readable In An eMail Or Web Page
' Try: SafeComment("`[email protected]#$%^&*()_+=-{}][|\'"";:<>?/.,")
    SafeComment = ""
    If Len(strInput) = 0 Then Exit Function
    SafeComment =   Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Server.HtmlEncode(Trim(strInput)), _
                    ":", "&#58;"), "-", "&#45;"), "|", "&#124;"), _
                    "`", "&#96;"), "(", "&#40;"), ")", "&#41;"), _
                    "%", "&#37;"), "^", "&#94;"), """", "&#34;"), _
                    "/", "&#47;"), "*", "&#42;"), "\", "&#92;"), _
                    "'", "&#39;")
End Function
分享给朋友:
您可能感兴趣的文章:
随机阅读: