Solved

Remove html tags when pasting to Textarea

Posted on 2007-12-06
12
2,224 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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 …

803 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