Solved

Remove html tags when pasting to Textarea

Posted on 2007-12-06
12
2,221 Views
Last Modified: 2008-02-01
I am using html editor so that when people enter text into the textarea field, they can format the text. But the problem is I don't want to allow people to cut and paste from MS Word to the textarea field with all the preformat text. Is there any way so that when people paste the text from MS Word to the textarea field, all the html tags will be removed automatically? Thanks for help
0
Comment
Question by:CochiseCounty
12 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20422630
Since the "source" is actually in the Clipboard when it is pasted then there is no easy way to tell it came from MS Word or not.  If you really had to do this then you would have to look at the html for tags used by and unique to MS Word.  If there are specific things you are trying to keep out then these would be good to search for.  Of course since MS Word can even create 2 versions of html code then this could be harder to detect if you want to catch both.

Let me know how this helps.  If you need any help with code then does this need to be done in the browser (i.e. with Javascript) or can it be done with server script?  Let me know if you have a question about anything.

bol
0
 

Author Comment

by:CochiseCounty
ID: 20423280
I prefer to do it with Javascripts. Like when people paste the the textarea, all the html tags are removed automatically.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 20423528
Are you looking to remove all html tags only or all formatting - i.e. end up with just plain text pasted.
0
 

Author Comment

by:CochiseCounty
ID: 20423599
yes, end up with just plain text pasted
0
 
LVL 12

Expert Comment

by:GoofyDawg
ID: 20423673
Unfortunately, there's not an easy way of doing that before they paste. IE has an onbeforepaste event, but it's a bit quirky to use. There's no such thing in other browsers.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20423772
Maybe I misunderstood.  You don't want an html at all?  If that is the case then this isn't hard.  Of course you could also just accept the html tags (if provided) an encode the to html entities.  Javascript or your server language could do this.  If you really want to strip all tags then don't depend on Javascript.  It can be disabled, etc and so your server script should really do this if you want to remove all html tags or make them "safe."

I thought you just wanted to remove the ugly (my word) html that MS Word makes, even using the filtered option.  Please clarify and let me know what you think of the above if you do want all gone.

bol
0
 

Author Comment

by:CochiseCounty
ID: 20427084
Ok, here's my situation. People in my office have to submit weekly report, the form they are using right now is an asp form, with a textarea field. When they paste into this field the text from a Word document, it removes all the html tags.
What I want is giving them the ability to format the text that they paste into the textarea field, and only alow, Bold, Italic, Underline, Bullets. So I replaced the textarea field with the htmleditor page, but the problem is when they paste into this field the text from a Word document, it keeps all the html tags. So I am trying to find a way sothat when they paste into the htmleditor field, all the html tags are removed.
Hope this help. And thanks for any help.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20430450
To remove all html from the contents of the textarea you would use ...

<textarea [your other tags] onblur="this.value = this.value.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, "");">

The Javascript is based on an expression I got and have use from JGSoft and their RegexBuddy program.  I don't know how well that will work in the htmlarea you are using.  You can try it as is but I don't know if it will remove some of the formatting you added with the htmlarea.  If there is a problem then let us know exactly what you use for the html textarea and provide a link to its website.

Remember, this won't try to see if it is MS Word html.  It will remove all html tags from the contents of the area.  Let me know if you have a question or need more info.

bol
0
 

Author Comment

by:CochiseCounty
ID: 20441541
OK, here is the js file that I am using for my htmlarea (it's from a web site, not mine). If I want to add something like Onpaste=this.value.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/ig, ""), where should I add. Thanks for help
//
// htmlArea v2.03 - Copyright (c) 2002 interactivetools.com, inc.
// This copyright notice MUST stay intact for use (see license.txt).
//
// A free WYSIWYG editor replacement for <textarea> fields.
// For full source code and docs, visit http://www.interactivetools.com/
//

** full code removed by b0lsc0tt **
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 250 total points
ID: 20447183
CochiseCounty,

Thanks for the info on the code.  I will try to look at it and see if I can find a solution that can use what I provided and go in the code.  I am afraid I probably won't have time to do that though.  It seems like it would be a major modification or at least would take a lot of time since I would have to learn how the script works.  If you have a link to some info you use to implement the code or a detailed explanation of it then post it.  That could make a difference.

Did you try just doing it like I suggested above?  I don't know if that would work but I am curious if you tried it want what was the result.

If I can't help and other experts don't join in then post a message in a day or two.  I will try to contact some other experts to see if they can/will help.  This is quite a bit to ask but maybe simpler than I see for someone who already knows the script.

Let me know if you have a question about any of this.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20586146
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

16 Experts available now in Live!

Get 1:1 Help Now