Solved

Text box which allows only certain html tags

Posted on 2008-10-29
8
780 Views
Last Modified: 2013-12-17
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
Comment
Question by:Kiran_M
  • 4
  • 4
8 Comments
 

Expert Comment

by:wolfdood
ID: 22831496
You could use the replace method.  For example... Replace("your string here", " <br />", "")
0
 

Author Comment

by:Kiran_M
ID: 22831584
Hmm...that was my first approach. How do i say ..replace all html tags in the string except ( tagA, TagB, Tag C).
0
 

Expert Comment

by:wolfdood
ID: 22831672
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
 

Expert Comment

by:wolfdood
ID: 22831709
Sorry, forgot to add when looping through each instance of the array just check for the tags you dont; want to remove.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Kiran_M
ID: 22831801
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
 

Assisted Solution

by:wolfdood
wolfdood earned 250 total points
ID: 22831894
Sorry, i only have the VB. Try using http://www.developerfusion.com/tools/convert/vb-to-csharp/ to convert it.
0
 

Author Comment

by:Kiran_M
ID: 22832084
Thank you. I'll give it a try and get back.
0
 

Accepted Solution

by:
Kiran_M earned 0 total points
ID: 22999320
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn on intranet settings 1 36
ASP.NET 5 Templates 2 65
IIS issues - Convert to Application - APS.NET v4.0 3 19
Automated testing suggestions? 2 22
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now