Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 800
  • Last Modified:

Text box which allows only certain html tags

I have a text box (multi line) on my aspx page.  This field needs to support only certain html tags (<B>.<U> and <i> and remove all other tags.  The code-behind is C#.  Any help with examples would be appreciated.


0
Kiran_M
Asked:
Kiran_M
  • 4
  • 4
2 Solutions
 
wolfdoodCommented:
You could use the replace method.  For example... Replace("your string here", " <br />", "")
0
 
Kiran_MAuthor Commented:
Hmm...that was my first approach. How do i say ..replace all html tags in the string except ( tagA, TagB, Tag C).
0
 
wolfdoodCommented:
How about this?
Function stripHTML(ByVal strHTML)
        'Strips the HTML tags from strHTML using split and join
 
        'Ensure that strHTML contains something
        If len(strHTML) = 0 Then
            stripHTML = strHTML
            Exit Function
        End If
 
        Dim arysplit, i, j, strOutput
 
        arysplit = split(strHTML, "<")
 
        'Assuming strHTML is nonempty, we want to start iterating
        'from the 2nd array postition
        If len(arysplit(0)) > 0 Then j = 1 Else j = 0
 
        'Loop through each instance of the array
        For i = j To ubound(arysplit)
            'Do we find a matching > sign?
            If instr(arysplit(i), ">") Then
                'If so, snip out all the text between the start of the string
                'and the > sign
                arysplit(i) = mid(arysplit(i), instr(arysplit(i), ">") + 1)
            Else
                arysplit(i) = "<" & arysplit(i)
            End If
        Next
 
        'Rejoin the array into a single string
        strOutput = join(arysplit, "")
 
        'Snip out the first <
        strOutput = mid(strOutput, 2 - j)
 
        'Convert < and > to &lt; and &gt;
        strOutput = replace(strOutput, ">", "&gt;")
        strOutput = replace(strOutput, "<", "&lt;")
 
        stripHTML = strOutput
    End Function

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
wolfdoodCommented:
Sorry, forgot to add when looping through each instance of the array just check for the tags you dont; want to remove.
0
 
Kiran_MAuthor Commented:
I came across this code online. However this strips all html tags. Let me try fine tuning this  code to ignore certain tags. Do you have C# equivalent?  Thanks.

0
 
wolfdoodCommented:
Sorry, i only have the VB. Try using http://www.developerfusion.com/tools/convert/vb-to-csharp/ to convert it.
0
 
Kiran_MAuthor Commented:
Thank you. I'll give it a try and get back.
0
 
Kiran_MAuthor Commented:
Woolfdood, the code convertor did a very poor job. I am having trouble changing the converted C# code to work. Basically i  need to check all the tags entered in the text and if it is not one of the allowed html tags, I need to throw an error message.

Got any other ideas?
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now