Solved

Remove html tags when pasting to Textarea

Posted on 2007-12-06
12
2,226 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to escape unsafe HTML tags 4 36
Video Tutorial help 2 37
Duplicate Elements with JS / jQuery 3 24
How to get chosen background-color on every line? 10 17
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

740 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