views:

70

answers:

4

Hello,

I have an HTML textarea element. I want to prevent a user from entering any HTML tags in this area. How do I detect if a user has entered any HTML a textarea with JavaScript?

Thank you

A: 

firstly, bear in mind that you'll need to re-validate on the server side, since anyone can fake a http post, and if they have javascript disabled then of course you have no control :)

what i'd do is

<textarea onkeypress="disableHtml(this);" name="text"></textarea>

and for the javascript

function disableHtml(element) {
  element.value = element.value.replace(/[<>]/g, '');
}

another way to do this would be to replace < and > with &lt; and &gt; on the server side, which is the better way because it's secure and people can still frown >:)

[edit : you can make the regexp as clever as you like, if you want to only detect certain tags for instance]

oedo
This will still leave the body of the HTML element — element name and attributes, etc. in cases where someone pasted in some markup.
Robusto
yep but it won't be valid html so it doesn't matter. server-side sanitisation is definitely the best policy anyway
oedo
This is a terrible method. As @robusto said but @odea ignored, simply pasting and submiting will get around this method.
Coronatus
there is no client-side method to avoid getting around it 100%. that's why i've continually advocated server-side stuff. @robusto said, if i understand, that if someone pastes in <a name="hi"> then it will still show a name="hi" without the < and >, that's all. or did i misunderstand you, robusto?
oedo
Server side validation is the best practice possible... If you're practicing client-side validation, why can't I just write a webpage to send you my username as `' DELETE * FROM * '`? [or in your case, </div></span></a></div></span></a></body></html><script> window.location="viruses.com"; </script>
ItzWarty
This is definitely unsafe against XSS.
BalusC
what's with all the downvotes? the solution does what OP wanted - to prevent people from entering any HTML tags in the textarea. i've also strongly suggested that they investigate a server-side solution as a safer method.
oedo
I have to -1 this, it's a horrendous solution. Aside from the reasons mentioned here (that this will only replace the `<` and `>` sign), imagine if someone enters `3 is > 1 but < 5`. The result is `3 is 1 but 5`
Andy E
@oedo: downvotes are used for a number of reasons and shouldn't really be taken personally. The solution you provided is flawed in many ways and should not be implemented at all.
Andy E
+3  A: 

One of the ways is to let the keypress event return false when the pressed key matches < or >. To distinguish real HTML tags from innocent "lesser than" and "greater than" signs, you may need to put some regex in. And since you can't parse HTML reliably with regex... There's however a jQuery way:

var sanitized = $('<div>').html(textareavalue).text();

The normal practice is however to just let the client enter whatever it want and sanitize HTML during display by the server side view technology in question. How to do it depends on the view technology you're using. In for example PHP you can use htmlspecialchars() for this and in JSP/JSTL the fn:escapeXml(). This is more robust since Javascript can be disabled/hacked/spoofed by the client.

BalusC
you also sanitize (in a different way) before saving to a database.
Adriano Varoli Piazza
Yes, against [SQL injections](http://en.wikipedia.org/wiki/SQL_injection).
BalusC
bobince
+1  A: 

You can use a regular expression, like

if ( textArea.value.match(/<\/*[a-z][^>]+?>/gi) ) {
  // do something about it
}

where "textArea" is the ID of your textarea element.

Robusto
+1  A: 

What can you consider as HTML tags? Is <b> a tag? What about the middle characters in I <3 how 5 is > 4?

I think you should not limit users with your strictness. Don't be a Steve Jobs.

Coronatus