Requires a clean / jQuery Javascript solution to clear Word HTML from text area

I know that this problem is raised here, but I have not yet found a viable solution for my situation, so I would like, but the brain returned the work and looked at what can be done.

I have textarea in a form that should detect when something is inserted into it and clear any hidden HTML and quotation marks. The contents of this form are emailed to a third-party system, which is particularly annoying, so sometimes even encoding the characters of an html object will not be a safe bet.

Unfortunately, I cannot use something like FCKEditor, TinyMCE, etc., in which case it should remain a normal text field. I tried to open the FCKEditor patch from the word function, but did not manage to trace it.

However, I can use the jQuery library if necessary, but have not yet found the jQuery plugin.

I am specifically looking for information designed to clean up embedded information, and not how to control an element to change content.

Any constructive help would be greatly appreciated.

+2
source share
5 answers

I look at the answer of David Archer, and he pretty much answers it. I used in the past a solution similar to it:

$("textarea").change( function() {
    // convert any opening and closing braces to their HTML encoded equivalent.
    var strClean = $(this).val().replace(/</gi, '&lt;').replace(/>/gi, '&gt;');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});

If you are looking for a way to completely DELETE HTML tags

$("textarea").change( function() {
    // Completely strips tags.  Taken from Prototype library.
    var strClean = $(this).val().replace(/<\/?[^>]+>/gi, '');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});
+8
source

Word HTML Cleaner. , , , , , , .

+5

- :

function cleanHTML(pastedString) {
    var cleanString = "";
    var insideTag = false;
    for (var i = 0, var len = pastedString.length; i < len; i++) {
        if (pastedString.charAt(i) == "<") insideTag = true;
        if (pastedString.charAt(i) == ">") {
            if (pastedString.charAt(i+1) != "<") {
                insideTag = false;
                i++;
            }
        }
        if (!insideTag) cleanString += pastedString.charAt(i);
    }
    return cleanString;
}

, .

+1

, :

$("textarea").blur(function() {
    // check input ($(this).val()) for validity here
});
+1

jquery..

$("textarea").change( function() {
    // check input ($(this).val()) for validity here
});

Thats . , ,

, ,

0

Source: https://habr.com/ru/post/1757724/


All Articles