0

I'm looking for a simple option to disallow any HTML entry. I want to prevent users from copy + pasting external HTML they find on the web as it always causes issues.

1
  • 1
    Wouldn't it be easier (and safer) to just encode the output? Commented Dec 28, 2012 at 1:50

1 Answer 1

0

This is a dirty job to do cross browser. For safari and chrome you could use the paste event. For example:

document.querySelector("div[contenteditable]").addEventListener("paste", function(e) {
    e.preventDefault();
    var text = e.clipboardData.getData("text/plain");
    var temp = document.createElement("div");
    temp.innerHTML = text;
    document.execCommand("insertHTML", false, temp.textContent);
});​

Same is possible with IE using onbeforepaste and/or onpaste. But the major problem is, that you can't access the clipboard in firefox. Some projects work with a hidden textarea and capture keyboard combinations like CTRL+V but as far as I know these solutions don't work very satisfying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.